request = \request(); } else { $this->request = $request; } } /** * Apply criteria in query repository * * @param string $model * @param RepositoryInterface $repository * * @return mixed */ public function apply($model, RepositoryInterface $repository) { if ($this->request->filled('name')) { $name = $this->request->get('name'); $model = $model->where('name', 'like', "%{$name}%"); } if ($this->request->filled('status')) { $status = $this->request->get('status', false); $model = $model->where('status', '=', $status); } if ($this->request->filled('category_id')) { $category_id = $this->request->get('category_id'); $model = $model->where('category_id', '=', $category_id); } if (!$this->request->filled('orderBy')) { $model = $model->orderByDesc('sort')->orderByDesc('id'); } return $model; } }