CollectCriteria.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. class CollectCriteria implements CriteriaInterface
  7. {
  8. /**
  9. * @var \Illuminate\Http\Request
  10. */
  11. protected $request;
  12. public function __construct(Request $request)
  13. {
  14. $this->request = $request;
  15. }
  16. /**
  17. * @param $model
  18. * @param RepositoryInterface $repository
  19. *
  20. * @return mixed
  21. */
  22. public function apply($model, RepositoryInterface $repository)
  23. {
  24. if ($this->request->filled('course_id')) {
  25. $val = $this->request->get('course_id');
  26. $model = $model->where('course_id', '=', $val);
  27. }
  28. if ($this->request->filled('status')) {
  29. $status = $this->request->get('status');
  30. $model = $model->where('status', '=', $status);
  31. }
  32. if ($this->request->filled('user_id')) {
  33. $val = $this->request->get('user_id');
  34. $model = $model->where('user_id', '=', $val);
  35. }
  36. if ($this->request->filled('ids')) {
  37. $ids = $this->request->get('ids');
  38. $model = $model->whereIn('id', $ids);
  39. }
  40. if (!$this->request->filled('orderBy')) {
  41. $model = $model->orderByDesc('id');
  42. }
  43. if (isApiModule()) {
  44. $model = $model->where('user_id', login_user_id());
  45. }
  46. return $model;
  47. }
  48. }