AttachCriteria.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Repositories\Criteria\Course;
  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 CourseAttachCriteria.
  9. *
  10. * @package namespace App\Repositories\Criteria;
  11. */
  12. class AttachCriteria implements CriteriaInterface
  13. {
  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('status', false)) {
  37. $status = $this->request->get('status');
  38. $model = $model->where('status', '=', $status);
  39. } else {
  40. $model = $model->where('status', '=', ModelStatusEnum::OK);
  41. }
  42. if ($this->request->filled('course_id')) {
  43. $course_id = $this->request->get('course_id');
  44. $model = $model->where('course_id', '=', $course_id);
  45. }
  46. if ($this->request->filled('video_id')) {
  47. $video_id = $this->request->get('video_id');
  48. $model = $model->where('course_video_id', '=', $video_id);
  49. }
  50. $model = $model->orderByDesc('sort')->orderBy('id');
  51. return $model;
  52. }
  53. }