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('title')) { $title = $this->request->get('title'); $model = $model->where('title', 'like', "%{$title}%"); } if ($this->request->filled('status')) { $status = $this->request->get('status'); $model = $model->where('status', '=', $status); } else { $model = $model->where('status', '=', ModelStatusEnum::OK); } if ($this->request->filled('category_id')) { $category_id = $this->request->get('category_id', false); $model = $model->where('category_id', '=', $category_id); } if ($this->request->filled('course_id')) { $course_id = $this->request->get('course_id', false); $model = $model->where('course_id', '=', $course_id); } if ($this->request->filled('video_id')) { $video_id = $this->request->get('video_id', false); $model = $model->where('video_id', '=', $video_id); } if ($this->request->filled('is_published')) { $is_published = $this->request->get('is_published', false); if ($is_published) { $model = $model->where('published_at', '<', Carbon::now()); } } if ($this->request->filled('is_me') && $this->request->get('is_me')) { $model = $model->where('user_id', '=', login_user_id()); } $model = $model->orderByDesc('id'); return $model; } }