ArticleCriteria.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Repositories\Criteria\CMS;
  3. use App\Repositories\Models\Inform\Category;
  4. use Illuminate\Http\Request;
  5. use Prettus\Repository\Contracts\CriteriaInterface;
  6. use Prettus\Repository\Contracts\RepositoryInterface;
  7. /**
  8. * Class ArticleCriteria.
  9. *
  10. * @package namespace App\Repositories\Criteria\CMS;
  11. */
  12. class ArticleCriteria 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. /**
  23. * Apply criteria in query repository
  24. *
  25. * @param string $model
  26. * @param RepositoryInterface $repository
  27. *
  28. * @return mixed
  29. */
  30. public function __construct(Request $request = null)
  31. {
  32. if (is_null($request)) {
  33. $this->request = \request();
  34. } else {
  35. $this->request = $request;
  36. }
  37. }
  38. /**
  39. * Apply criteria in query repository
  40. *
  41. * @param string $model
  42. * @param RepositoryInterface $repository
  43. *
  44. * @return mixed
  45. */
  46. public function apply($model, RepositoryInterface $repository)
  47. {
  48. if ($title = $this->request->get('title')) {
  49. $model = $model->where('title', 'like', "%{$title}%");
  50. }
  51. if (($status = $this->request->get('status', false)) !== false) {
  52. $model = $model->where('status', '=', $status);
  53. }
  54. if ($category_id = $this->request->get('category_id')) {
  55. $ids = Category::sonGroup($category_id);
  56. $ids[] = $category_id;
  57. $model = $model->whereIn('category_id', $ids);
  58. }
  59. $model = $model->orderByDesc('sort')->orderByDesc('published_at')->orderByDesc('id');
  60. return $model;
  61. }
  62. }