InformationCriteria.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 ($this->request->filled('title')) {
  41. $title = $this->request->get('title');
  42. if ($title) $model = $model->where('title', 'like', "%{$title}%");
  43. }
  44. if ($this->request->filled('status')) {
  45. $status = $this->request->get('status');
  46. $model = $model->where('status', '=', $status);
  47. }
  48. if ($this->request->filled('category_id')) {
  49. $category_id = $this->request->get('category_id');
  50. if ($category_id) $model = $model->where('category_id', $category_id);
  51. }
  52. if ($organization_id = $this->request->get('organization_id')) {
  53. if ($organization_id) {
  54. $model = $model->where('organization_id', $organization_id);
  55. } else {
  56. $model = $model->where('organization_id', 0);
  57. }
  58. } else {
  59. $model = $model->where('organization_id', 0);
  60. }
  61. if ($tag = $this->request->get('tag')) {
  62. switch ($tag) {
  63. case 'new':
  64. $model = $model->orderByDesc('id');
  65. break;
  66. case 'hot':
  67. $model = $model->orderByDesc('view_count')->orderByDesc('id');
  68. break;
  69. case 'good':
  70. $model = $model->orderByDesc('good_count')->orderByDesc('id');
  71. break;
  72. }
  73. } else {
  74. $model = $model->orderByDesc('sort')->orderByDesc('id');
  75. }
  76. $model = $model->orderByDesc('sort')->orderByDesc('id');
  77. return $model;
  78. }
  79. }