request = $request; $this->where = $where; $this->relations = $relations; } /** * Apply criteria in query repository * * @param Builder|Model $model * @param RepositoryInterface $repository * * @throws \Exception * * @return mixed */ public function apply($model, RepositoryInterface $repository) { if ($this->where) { $model = $model->where($this->where); } // See if any relationships need to be included in this WHERE if ($this->relations) { foreach ($this->relations as $relation => $criterea) { $model = $model ->with($relation) ->whereHas($relation, function (Builder $query) use ($criterea) { $query->where($criterea); }); } } return $model; } }