InformationCriteria.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Repositories\Criteria\Inform;
  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 InformationCriteria.
  9. *
  10. * @package namespace App\Repositories\Criteria\Inform;
  11. */
  12. class InformationCriteria 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. public function __construct(Request $request = null)
  23. {
  24. if (is_null($request)) {
  25. $this->request = \request();
  26. } else {
  27. $this->request = $request;
  28. }
  29. }
  30. /**
  31. * Apply criteria in query repository
  32. *
  33. * @param string $model
  34. * @param RepositoryInterface $repository
  35. *
  36. * @return mixed
  37. */
  38. public function apply($model, RepositoryInterface $repository)
  39. {
  40. if ($title = $this->request->get('title')) {
  41. $model = $model->where('title', 'like', "%{$title}%");
  42. }
  43. if (($status = $this->request->get('status', false)) !== false) {
  44. $model = $model->where('status', '=', $status);
  45. }
  46. if ($category_id = $this->request->get('category_id')) {
  47. $ids = Category::sonGroup($category_id);
  48. $ids[] = $category_id;
  49. $model = $model->whereIn('category_id', $ids);
  50. }
  51. if ($tag = $this->request->get('tag')) {
  52. switch ($tag) {
  53. case 'new':
  54. $model = $model->orderByDesc('id');
  55. break;
  56. case 'hot':
  57. $model = $model->orderByDesc('view_count')->orderByDesc('id');
  58. break;
  59. case 'good':
  60. $model = $model->orderByDesc('good_count')->orderByDesc('id');
  61. break;
  62. }
  63. } else {
  64. $model = $model->orderByDesc('sort')->orderByDesc('id');
  65. }
  66. $user = login_admin();
  67. $user_id = $user->id;
  68. if ($user_id > 1) {
  69. $model = $model->whereIn('category_id', Category::byRoleGetIds($user->role_id));
  70. }
  71. $model = $model->orderByDesc('sort')->orderByDesc('id');
  72. return $model;
  73. }
  74. }