123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Repositories\Criteria\CMS;
- use App\Repositories\Models\Inform\Category;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class ArticleCriteria.
- *
- * @package namespace App\Repositories\Criteria\CMS;
- */
- class ArticleCriteria implements CriteriaInterface
- {
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- /**
- * Apply criteria in query repository
- *
- * @param string $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function __construct(Request $request = null)
- {
- if (is_null($request)) {
- $this->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 ($title = $this->request->get('title')) {
- $model = $model->where('title', 'like', "%{$title}%");
- }
- if (($status = $this->request->get('status', false)) !== false) {
- $model = $model->where('status', '=', $status);
- }
- if ($category_id = $this->request->get('category_id')) {
- $ids = Category::sonGroup($category_id);
- $ids[] = $category_id;
- $model = $model->whereIn('category_id', $ids);
- }
- $model = $model->orderByDesc('sort')->orderByDesc('published_at')->orderByDesc('id');
- return $model;
- }
- }
|