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.

Home Page:https://bosnadev.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple Criteria in Controller method

kskrlin opened this issue · comments

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?

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

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;
    }
...