BannerCriteria.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Repositories\Criteria\CMS;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Prettus\Repository\Contracts\CriteriaInterface;
  6. use Prettus\Repository\Contracts\RepositoryInterface;
  7. /**
  8. * Class BannerCriteria.
  9. *
  10. * @package namespace App\Repositories\Criteria\CMS;
  11. */
  12. class BannerCriteria implements CriteriaInterface
  13. {
  14. /**
  15. * Apply criteria in query repository
  16. *
  17. * @param string $model
  18. * @param RepositoryInterface $repository
  19. *
  20. * @return mixed
  21. */
  22. public function __construct(Request $request = null)
  23. {
  24. if (is_null($request)) {
  25. $this->request = \request();
  26. } else {
  27. $this->request = $request;
  28. }
  29. }
  30. /**
  31. * Apply criteria in query repository
  32. *
  33. * @param string $model
  34. * @param RepositoryInterface $repository
  35. *
  36. * @return mixed
  37. */
  38. public function apply($model, RepositoryInterface $repository)
  39. {
  40. if ($name = $this->request->get('name')) {
  41. $model = $model->where('name', 'like', "%{$name}%");
  42. }
  43. if (($status = $this->request->get('status', false)) !== false) {
  44. $model = $model->where('status', '=', $status);
  45. }
  46. if ($position = $this->request->get('position')) {
  47. $model = $model->where('position', '=', "{$position}");
  48. }
  49. if ($this->request->get('is_user', 0)) {
  50. $now = Carbon::now()->toDateTimeString();
  51. $model = $model->where('start_time', '<=', $now)->where('end_time', '>', $now);
  52. }
  53. $model = $model->orderByDesc('sort')->orderByDesc('id');
  54. return $model;
  55. }
  56. }