123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Repositories\Criteria\CMS;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- /**
- * Class BannerCriteria.
- *
- * @package namespace App\Repositories\Criteria\CMS;
- */
- class BannerCriteria implements CriteriaInterface
- {
- /**
- * 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 ($name = $this->request->get('name')) {
- $model = $model->where('name', 'like', "%{$name}%");
- }
- if (($status = $this->request->get('status', false)) !== false) {
- $model = $model->where('status', '=', $status);
- }
- if ($position = $this->request->get('position')) {
- $model = $model->where('position', '=', "{$position}");
- }
- if ($this->request->get('is_user', 0)) {
- $now = Carbon::now()->toDateTimeString();
- $model = $model->where('start_time', '<=', $now)->where('end_time', '>', $now);
- }
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- return $model;
- }
- }
|