LessonCriteria.php 1.9 KB

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