TopicCriteria.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Repositories\Criteria\Exam;
  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 TopicCriteria.
  9. *
  10. * @package namespace App\Repositories\Criteria\Exam;
  11. */
  12. class TopicCriteria implements CriteriaInterface
  13. {
  14. /**
  15. * Apply criteria in query repository
  16. *
  17. * @param string $model
  18. * @param RepositoryInterface $repository
  19. *
  20. * @return mixed
  21. */
  22. public function __construct(Request $request = null)
  23. {
  24. if (is_null($request)) {
  25. $this->request = \request();
  26. } else {
  27. $this->request = $request;
  28. }
  29. }
  30. /**
  31. * Apply criteria in query repository
  32. *
  33. * @param string $model
  34. * @param RepositoryInterface $repository
  35. *
  36. * @return mixed
  37. */
  38. public function apply($model, RepositoryInterface $repository)
  39. {
  40. if ($this->request->filled('title')) {
  41. $title = $this->request->get('title');
  42. $model = $model->where('title', 'like', "%{$title}%");
  43. }
  44. if ($this->request->filled('status')) {
  45. $status = $this->request->get('status');
  46. $model = $model->where('status', '=', $status);
  47. } else {
  48. $model = $model->where('status', '=', ModelStatusEnum::OK);
  49. }
  50. if ($this->request->filled('category_id')) {
  51. $category_id = $this->request->get('category_id', false);
  52. $model = $model->where('category_id', '=', $category_id);
  53. }
  54. if ($this->request->filled('type')) {
  55. $type = $this->request->get('type', false);
  56. $model = $model->where('type', '=', $type);
  57. }
  58. if ($this->request->filled('level')) {
  59. $level = $this->request->get('level', false);
  60. $model = $model->where('level', '=', $level);
  61. }
  62. if ($this->request->filled('point')) {
  63. $point = $this->request->get('point', false);
  64. $model = $model->where('point', '=', $point);
  65. }
  66. $model = $model->orderByDesc('id');
  67. return $model;
  68. }
  69. }