UserCriteria.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Repositories\Criteria\User;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. /**
  7. * Class StudentCriteria.
  8. *
  9. * @package namespace App\Repositories\Criteria\User\Student;
  10. */
  11. class UserCriteria implements CriteriaInterface
  12. {
  13. /**
  14. * Apply criteria in query repository
  15. *
  16. * @param string $model
  17. * @param RepositoryInterface $repository
  18. *
  19. * @return mixed
  20. */
  21. public function __construct(Request $request = null)
  22. {
  23. if (is_null($request)) {
  24. $this->request = \request();
  25. } else {
  26. $this->request = $request;
  27. }
  28. }
  29. /**
  30. * Apply criteria in query repository
  31. *
  32. * @param string $model
  33. * @param RepositoryInterface $repository
  34. *
  35. * @return mixed
  36. */
  37. public function apply($model, RepositoryInterface $repository)
  38. {
  39. if ($this->request->filled('turename')) {
  40. $turename = $this->request->get('turename');
  41. $model = $model->where('turename', 'like', "%{$turename}%");
  42. }
  43. if ($this->request->filled('username')) {
  44. $username = $this->request->get('username');
  45. $model = $model->where('username', 'like', "%{$username}%");
  46. }
  47. if ($this->request->filled('mobile')) {
  48. $mobile = $this->request->get('mobile');
  49. $model = $model->where('mobile', 'like', "%{$mobile}%");
  50. }
  51. if ($this->request->filled('class')) {
  52. $class = $this->request->get('class');
  53. $model = $model->where('class', 'like', "%{$class}%");
  54. }
  55. if ($this->request->filled('name')) {
  56. $name = $this->request->get('name');
  57. $model = $model->where('name', 'like', "%{$name}%");
  58. }
  59. if ($this->request->filled('sex')) {
  60. $sex = $this->request->get('sex');
  61. $model = $model->where('sex', '=', $sex);
  62. }
  63. if ($this->request->filled('role_id')) {
  64. $role_id = $this->request->get('role_id');
  65. $model = $model->where('role_id', '=', $role_id);
  66. }
  67. // if ($period = $this->request->get('period')) {
  68. // $model = $model->where('period', 'like', $period);
  69. // }
  70. if ($this->request->filled('organization_id')) {
  71. $organization_id = $this->request->get('organization_id');
  72. $model = $model->whereHas('organizations', function ($query) use ($organization_id) {
  73. return $query->where('organization_id', $organization_id);
  74. });
  75. }
  76. $model = $model->orderByDesc('id');
  77. return $model;
  78. }
  79. }