AreaCriteria.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Repositories\Criteria\Area;
  3. use App\Repositories\Enums\Base\RoleEnum;
  4. use App\Repositories\Enums\ModelStatusEnum;
  5. use Illuminate\Http\Request;
  6. use Prettus\Repository\Contracts\CriteriaInterface;
  7. use Prettus\Repository\Contracts\RepositoryInterface;
  8. /**
  9. * Class AreaCriteria.
  10. *
  11. * @package namespace App\Repositories\Criteria\Area;
  12. */
  13. class AreaCriteria implements CriteriaInterface
  14. {
  15. /**
  16. * Apply criteria in query repository
  17. *
  18. * @param string $model
  19. * @param RepositoryInterface $repository
  20. *
  21. * @return mixed
  22. */
  23. public function __construct(Request $request = null)
  24. {
  25. if (is_null($request)) {
  26. $this->request = \request();
  27. } else {
  28. $this->request = $request;
  29. }
  30. }
  31. /**
  32. * Apply criteria in query repository
  33. *
  34. * @param string $model
  35. * @param RepositoryInterface $repository
  36. *
  37. * @return mixed
  38. */
  39. public function apply($model, RepositoryInterface $repository)
  40. {
  41. if ($this->request->filled('name')) {
  42. $name = $this->request->get('name');
  43. $model = $model->where('name', 'like', "%{$name}%");
  44. }
  45. if ($this->request->filled('status')) {
  46. $status = $this->request->get('status');
  47. $model = $model->where('status', '=', $status);
  48. } else {
  49. $model = $model->where('status', '=', ModelStatusEnum::OK);
  50. }
  51. if ($this->request->filled('project_id')) {
  52. $project_id = $this->request->get('project_id', false);
  53. $model = $model->where('project_id', '=', $project_id);
  54. }
  55. if ($this->request->filled('type')) {
  56. $type = $this->request->get('type', false);
  57. $model = $model->where('type', '=', $type);
  58. }
  59. if (is_admin_login()) {
  60. $admin = login_admin();
  61. if ($admin->role_id > RoleEnum::ADMIN) {
  62. $model = $model->where('shop_id', $admin->shop_id);
  63. }
  64. }
  65. $model = $model->orderByDesc('id');
  66. return $model;
  67. }
  68. }