bosnadev / repository

Laravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain.
https://bosnadev.com
825 stars 235 forks source link

Multiple Criteria in Controller method #87

Open kskrlin opened 8 years ago

kskrlin commented 8 years ago

How to call more then 1 Criteria inside the controller method? For example, I want to get separated active and inactive users (status is checked between 2 tables and a few where clauses). If I use: $active = $this->user->getByCriteria(new GetActiveUsers($role_id))->all(); and $inactive = $this->user->getByCriteria(new GetInactiveUsers($role_id))->all(); the second one just appends Criteria GetInactiveUsers on the above query.

How to reset query data in this example?

coreyar commented 7 years ago

I am also curious how this might be done. Currently trying to find a solution and will update.

coreyar commented 7 years ago

Here is my solution. I added a resetCriteria function to the base Repository class.

I replace Criteria with an empty Collection, apply it, then recreate the model.

...
public function resetCriteria()
    {
        $this->criteria = new Collection;
        $this->applyCriteria();
        $this->makeModel();
        return $this;
    }
...