12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace App\Repositories\Criteria\Base;
- use App\Repositories\Enums\ModelStatusEnum;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Prettus\Repository\Contracts\CriteriaInterface;
- use Prettus\Repository\Contracts\RepositoryInterface;
- class BannerCriteria implements CriteriaInterface
- {
- /**
- * @var \Illuminate\Http\Request
- */
- protected $request;
- public function __construct(Request $request)
- {
- $this->request = $request;
- }
- /**
- * @param $model
- * @param RepositoryInterface $repository
- *
- * @return mixed
- */
- public function apply($model, RepositoryInterface $repository)
- {
- if ($this->request->filled('type')) {
- $type = $this->request->get('type');
- $model = $model->where('type', '=', $type);
- }
- if ($this->request->filled('name')) {
- $name = $this->request->get('name');
- $model = $model->where('name', 'like', "%{$name}%");
- }
- if ($this->request->filled('is_show')) {
- $time = Carbon::now()->toDateTimeString();
- $model = $model->where('start_time', '<=', $time)->where('end_time', '>', $time)->where('status', '=', ModelStatusEnum::OK);
- }
- if ($this->request->filled('status')) {
- $status = $this->request->get('status');
- $model = $model->where('status', '=', $status);
- }
- if (!$this->request->filled('orderBy')) {
- $model = $model->orderByDesc('sort')->orderByDesc('id');
- }
- return $model;
- }
- }
|