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', 0); $model = $model->where('status', '=', $status); } if ($this->request->filled('category_id')) { $category_id = $this->request->get('category_id', 0); $model = $model->where('category_id', '=', $category_id); } if ($this->request->filled('tag')) { $tag = $this->request->get('tag'); switch ($tag) { case 'new': $model = $model->orderByDesc('id'); break; case 'hot': $model = $model->orderByDesc('user_count')->orderByDesc('id'); break; case 'rec': $model = $model->orderByDesc('is_rec')->orderByDesc('id'); break; } } else { if (!$this->request->filled('orderBy')) { $model = $model->orderByDesc('sort')->orderByDesc('id'); } } if (isApi()) { $model = $model->where('status', ModelStatusEnum::OK)->where('published_at', '<=', Carbon::now()->toDateTimeString()); } return $model; } }