BusinessSchoolCriteria.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Repositories\Criteria\Dwbs;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use Illuminate\Http\Request;
  5. use Prettus\Repository\Contracts\CriteriaInterface;
  6. use Prettus\Repository\Contracts\RepositoryInterface;
  7. class BusinessSchoolCriteria implements CriteriaInterface
  8. {
  9. /**
  10. * @var \Illuminate\Http\Request
  11. */
  12. protected $request;
  13. public function __construct(Request $request)
  14. {
  15. $this->request = $request;
  16. }
  17. /**
  18. * @param $model
  19. * @param RepositoryInterface $repository
  20. *
  21. * @return mixed
  22. */
  23. public function apply($model, RepositoryInterface $repository)
  24. {
  25. if ($this->request->filled('type')) {
  26. $val = $this->request->get('type');
  27. $model = $model->where('type', '=', $val);
  28. }
  29. if ($this->request->filled('day')) {
  30. $val = $this->request->get('day');
  31. $model = $model->where('day', '=', $val);
  32. }
  33. if ($this->request->filled('category_id')) {
  34. $val = $this->request->get('category_id');
  35. if ($val) $model = $model->where('category_id', '=', $val);
  36. }
  37. if ($this->request->filled('name')) {
  38. $val = $this->request->get('name');
  39. $model = $model->where('name', 'like', "%{$val}%");
  40. }
  41. if ($this->request->filled('status')) {
  42. $status = $this->request->get('status');
  43. $model = $model->where('status', '=', $status);
  44. }
  45. if ($this->request->filled('ids')) {
  46. $ids = $this->request->get('ids');
  47. $model = $model->whereIn('id', $ids);
  48. }
  49. if (!$this->request->filled('orderBy')) {
  50. $model = $model->orderByDesc('id');
  51. }
  52. if (!isAdminModule()) $model = $model->where('status', ModelStatusEnum::OK);
  53. return $model;
  54. }
  55. }