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'); if ($title) $model = $model->where('title', 'like', "%{$title}%"); } if ($this->request->filled('status')) { $status = $this->request->get('status'); $model = $model->where('status', '=', $status); } if ($this->request->filled('category_id')) { $category_id = $this->request->get('category_id'); if ($category_id) $model = $model->where('category_id', $category_id); } if ($organization_id = $this->request->get('organization_id')) { if ($organization_id) { $model = $model->where('organization_id', $organization_id); } else { $model = $model->where('organization_id', 0); } } else { $model = $model->where('organization_id', 0); } if ($tag = $this->request->get('tag')) { switch ($tag) { case 'new': $model = $model->orderByDesc('id'); break; case 'hot': $model = $model->orderByDesc('view_count')->orderByDesc('id'); break; case 'good': $model = $model->orderByDesc('good_count')->orderByDesc('id'); break; } } else { $model = $model->orderByDesc('sort')->orderByDesc('id'); } $model = $model->orderByDesc('sort')->orderByDesc('id'); return $model; } }