ChapterCriteria.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Repositories\Criteria\Course;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. /**
  7. * Class CourseChapterCriteria.
  8. *
  9. * @package namespace App\Repositories\Criteria;
  10. */
  11. class ChapterCriteria implements CriteriaInterface
  12. {
  13. public function __construct(Request $request = null)
  14. {
  15. if (is_null($request)) {
  16. $this->request = \request();
  17. } else {
  18. $this->request = $request;
  19. }
  20. }
  21. /**
  22. * Apply criteria in query repository
  23. *
  24. * @param string $model
  25. * @param RepositoryInterface $repository
  26. *
  27. * @return mixed
  28. */
  29. public function apply($model, RepositoryInterface $repository)
  30. {
  31. if ($title = $this->request->get('title')) {
  32. $model = $model->where('title', 'like', "%{$title}%");
  33. }
  34. if (($status = $this->request->get('status', false)) !== false) {
  35. $model = $model->where('status', '=', $status);
  36. }
  37. if ($course_id = $this->request->get('course_id')) {
  38. $model = $model->where('course_id', '=', $course_id);
  39. }
  40. $model = $model->orderByDesc('sort')->orderBy('id');
  41. return $model;
  42. }
  43. }