NoteCriteria.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Repositories\Criteria\Note;
  3. use Illuminate\Http\Request;
  4. use Prettus\Repository\Contracts\CriteriaInterface;
  5. use Prettus\Repository\Contracts\RepositoryInterface;
  6. /**
  7. * Class NoteCriteria.
  8. *
  9. * @package namespace App\Repositories\Criteria\Note;
  10. */
  11. class NoteCriteria 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('name', 'like', "%{$title}%");
  33. }
  34. if ($course_category_id = $this->request->get('course_category_id')) {
  35. $model = $model->where('course_category_id', '=', $course_category_id);
  36. }
  37. if ($tags = $this->request->get('tags')) {
  38. $model = $model->where('tags', 'like', "%-{$tags}-%");
  39. }
  40. if (($status = $this->request->get('status', false)) !== false) {
  41. $model = $model->where('status', '=', $status);
  42. }
  43. if ($id = $this->request->get('id')) {
  44. $model = $model->where('id', '=', $id);
  45. }
  46. if (($is_release = $this->request->get('is_release', false)) !== false) {
  47. $model = $model->where('is_release', '=', $is_release);
  48. }
  49. if (($type = $this->request->get('type', false)) !== false) {
  50. $model = $model->where('type', '=', $type);
  51. }
  52. if ($this->request->get('me')) {
  53. $model = $model->where('user_id', login_user_id());
  54. }
  55. if ($tag = $this->request->get('tag')) {
  56. switch ($tag) {
  57. case 'new':
  58. $model = $model->orderByDesc('id');
  59. break;
  60. case 'hot':
  61. $model = $model->orderByDesc('good_count')->orderByDesc('id');
  62. break;
  63. }
  64. } else {
  65. $model = $model->orderByDesc('id');
  66. }
  67. return $model;
  68. }
  69. }