AdminCriteria.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Repositories\Criteria\Base;
  3. use App\Repositories\Enums\Base\AdminTypeEnum;
  4. use Illuminate\Http\Request;
  5. use Prettus\Repository\Contracts\CriteriaInterface;
  6. use Prettus\Repository\Contracts\RepositoryInterface;
  7. class AdminCriteria 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('name')) {
  26. $model = $model->where('name', 'like', '%' . $this->request->get('name') . '%');
  27. }
  28. if ($this->request->filled('username')) {
  29. $model = $model->where('username', 'like', '%' . $this->request->get('username') . '%');
  30. }
  31. if ($this->request->filled('mobile')) {
  32. $model = $model->where('mobile', 'like', '%' . $this->request->get('mobile') . '%');
  33. }
  34. if ($this->request->filled('email')) {
  35. $model = $model->where('email', 'like', '%' . $this->request->get('email') . '%');
  36. }
  37. if ($this->request->filled('sex')) {
  38. $model = $model->where('sex', '=', $this->request->get('sex'));
  39. }
  40. if ($this->request->filled('department_id')) {
  41. $model = $model->where('department_id', '=', $this->request->get('department_id'));
  42. }
  43. if ($this->request->filled('status')) {
  44. $model = $model->where('status', '=', $this->request->get('status'));
  45. }
  46. if ($this->request->filled('type')) {
  47. $model = $model->where('type', '=', $this->request->get('type'));
  48. }
  49. if ($this->request->filled('company_id')) {
  50. $model = $model->where('company_id', '=', $this->request->get('company_id'));
  51. }
  52. if ($this->request->filled('role_name')) {
  53. $role_name = $this->request->get('role_name');
  54. $model = $model->whereHas('roles', function ($query) use ($role_name) {
  55. return $query->where('name', $role_name);
  56. });
  57. }
  58. if ($this->request->filled('role_id')) {
  59. $role_id = $this->request->get('role_id');
  60. $model = $model->whereHas('roles', function ($query) use ($role_id) {
  61. return $query->where('id', $role_id);
  62. });
  63. }
  64. if ($this->request->filled('name')) {
  65. $model = $model->where('name', 'like', '%' . $this->request->get('name') . '%');
  66. }
  67. if ($this->request->filled('user_no')) {
  68. $model = $model->where('user_no', 'like', '%' . $this->request->get('user_no'));
  69. }
  70. if ($this->request->filled('mobile')) {
  71. $model = $model->where('mobile', 'like', '%' . $this->request->get('mobile'));
  72. }
  73. if (!$this->request->filled('orderBy')) {
  74. $model = $model->orderBy('id');
  75. }
  76. $model = $model->where('type', '=', AdminTypeEnum::ADMIN);
  77. return $model;
  78. }
  79. }