StudentCriteria.php 2.1 KB

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