aaronjan / housekeeper Goto Github PK
View Code? Open in Web Editor NEWPowerful, simple Repository-Pattern implementation for Laravel (>=5.1), and it come with tests.
License: Apache License 2.0
Powerful, simple Repository-Pattern implementation for Laravel (>=5.1), and it come with tests.
License: Apache License 2.0
Hello,
I am trying your package. Seems great !
How would you add a whereHas constraint to the query ? (could be withTrashed())
I tried to create a criteria, but the model property is protected and therefore inaccessible.
E.g :
/**
* The constructor
* @param string $relation
* @param string $attribute
* @param string $value
* @param string $operator
*/
public function __construct($relation, $attribute, $value, $operator = '=')
{
$this->relation = $relation;
$this->attribute = $attribute;
$this->value = $value;
$this->operator = $operator;
}
/**
* Apply the criteria
* @param RepositoryInterface $repository
*/
public function apply(RepositoryInterface $repository)
{
// Determine whether 'and' or 'or'
$method = is_null($this->or) ? 'whereHas' : 'orWhereHas';
// Get the the attributes as an array
$relations = explode('.', $this->relation);
// Create closure for the whereHas method
$closure = function($q) use(&$closure, &$relations)
{
if (count($relations) > 0)
{
$q->whereHas(array_shift($relations), $closure);
}
else
{
$q->where($this->attribute, $this->operator, $this->value);
}
};
// Apply the criteria to the model
$repository->model->{$method}(array_shift($relations), $closure);
}
EDIT : I need a criteria since i want to be able to call paginate() after applying this criteria. Looking at your code, I can't see how I can do this with findWhere() or findByFields() (they both call get()).
Thanks.
Hello,
I am wondering why the Before and After injections are executed before and after the required method (e.g update(...)) and not within the method ?
To be clear, with the way the flow works, you can't modify the model itself right ? Or did I miss something ?
Example :
Assuming I want to create a new record with a relation.
public function create(array $attributes)
{
return $this->wrap(function ($attributes) {
/////// -- The instance is created after the BEFORE injections -- ///////
/////// -- We can not modify the model instance before save then ? -- ///////
$model = $this->model->newInstance($attributes);
/**
* @var Model $model
*/
$model->save();
return $model;
}, new Action(__METHOD__, func_get_args(), Action::CREATE));
}
So if I want to pass the relation as an argument, I can't create a BEFORE flow that automatically associates the relation to the model.
Of course, I could do it after the call to the repository in my controller / command (or whatever) but :
Same issue about the modification of the arguments ? Ex : modify an argument before saving the model.
Thanks,
Hello,
Found another issue.
Assuming you have a model that can be softDelete with the SoftDeletes trait.
Every queries built with eloquent will have the following statement : where
model.
deleted_at is null
Now, if we want to get the trashed, we obviously need a criteria (so we don't have to extend the BaseRepository class and change everything around).
But a criteria only allows us to change the query with the "query builder" (not eloquent). And the withTrashed() method is not available for the query builder.
public function apply(RepositoryInterface $repository)
{
// Create the closure
$closure = function($q)
{
// Won't work because $q-> gives us the query builder not eloquent
$q->withTrashed();
};
// Apply the criteria to the model
$repository->applyWhere([$closure]);
}
You either need to implement the softDeletes feature or gives us access to the model instance in a criteria.
By only allowing us to access the query builder we are not able to perform several actions coupled to eloquent (like softDeletes).
Thanks,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.