Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. <?php
  2. namespace App;
  3. use Illuminate\Support\Facades\DB;
  4. class ModSorting
  5. {
  6. protected $request;
  7. protected $builder;
  8. public $filters;
  9. public $types;
  10. public $search;
  11. public function __construct($request, $builder)
  12. {
  13. $this->request = $request;
  14. $this->builder = $builder;
  15. }
  16. public function apply()
  17. {
  18. return $this->builder;
  19. }
  20. public function getFilters()
  21. {
  22. return $this->request->all();
  23. }
  24. public function getMods()
  25. {
  26. $this->builder = $this->builder->where('show_in_list','=', 1);
  27. $this->apply();
  28. foreach ($this->getFilters() as $filter => $value) {
  29. if (method_exists($this, $filter)) {
  30. $this->$filter($value);
  31. }
  32. }
  33. return $this->builder;
  34. }
  35. public function type($value)
  36. {
  37. $types = explode(',',$value);
  38. $this->types = $types;
  39. // Создаем функцию, чтобы в зависиммости от выбора типа выборки, подставлялись нужные значения в builder
  40. function getTypes ($q, $tt) {
  41. foreach ($tt as $type){
  42. switch ($type){
  43. case '1':
  44. $q->orWhereColumn('count','<','min_count');
  45. break;
  46. case '2':
  47. $q->orWhereColumn([
  48. ['count','<=','reg_count'],
  49. ['count','>=','min_count']
  50. ]);
  51. break;
  52. case '3':
  53. $q->orWhereColumn('count','>','reg_count');
  54. break;
  55. case '4' :
  56. $q->whereNotNull('info');
  57.  
  58. }
  59. }
  60. }
  61. // Вклдючаем выборку по параметрам типов
  62. $this->builder->where(function ($q){
  63. getTypes($q, $this->types);
  64. });
  65. }
  66. public function category($value) {
  67. $this->builder = $this->builder->where('parent', '=', $value);
  68. return $this->builder;
  69. }
  70. public function search($value) {
  71. $this->search = $value;
  72. function getSearch($q, $value) {
  73. $q->where('pagetitle', 'LIKE', "%$value%")->orWhere('value', 'LIKE', "%$value%");
  74. }
  75. if ($value) {
  76. //Рабочий метод
  77. $this->builder = $this->builder->where(function ($q){
  78. getSearch($q, $this->search);
  79. });
  80. return $this->builder;
  81. }
  82. }
  83.  
  84. public function status($value)
  85. {
  86. if ($value != 0) {
  87. $this->builder = $this->builder->where('status', '=', $value);
  88. }else {
  89. $this->builder->whereNotNull('status');
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement