@@ -85,12 +85,12 @@ discard block |
||
85 | 85 | if(count($_searchable)>1): |
86 | 86 | |
87 | 87 | $query->with($_searchable[0])->orWhereHas($_searchable[0], function($query) use ( $_searchable ) |
88 | - { |
|
89 | - $query->where($_searchable[1],'LIKE','%'.$this->search.'%'); |
|
90 | - }); |
|
91 | - else: |
|
88 | + { |
|
89 | + $query->where($_searchable[1],'LIKE','%'.$this->search.'%'); |
|
90 | + }); |
|
91 | + else: |
|
92 | 92 | $query->orWhere($searchable,'LIKE','%'.$this->search.'%'); |
93 | - endif; |
|
93 | + endif; |
|
94 | 94 | |
95 | 95 | endforeach; |
96 | 96 | }); |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | |
190 | 190 | $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values, $value) |
191 | 191 | { |
192 | - $query->whereBetween($relationship[1],$values); |
|
192 | + $query->whereBetween($relationship[1],$values); |
|
193 | 193 | }); |
194 | 194 | |
195 | 195 | endif; |
@@ -4,25 +4,25 @@ discard block |
||
4 | 4 | |
5 | 5 | trait Traversify |
6 | 6 | { |
7 | - public $search; # Search keyword (String) |
|
7 | + public $search; # Search keyword (String) |
|
8 | 8 | |
9 | - public $filter; # Query filters (Array) |
|
9 | + public $filter; # Query filters (Array) |
|
10 | 10 | |
11 | - public $custom; # Custom queries |
|
11 | + public $custom; # Custom queries |
|
12 | 12 | |
13 | - public $take; # For limited show (Integer) |
|
13 | + public $take; # For limited show (Integer) |
|
14 | 14 | |
15 | - public $order; # Order of list (Array) |
|
15 | + public $order; # Order of list (Array) |
|
16 | 16 | |
17 | - public $range; # Range of query (Array) |
|
17 | + public $range; # Range of query (Array) |
|
18 | 18 | |
19 | - public $limit; # Limit show - Paginate Prerequesit () |
|
19 | + public $limit; # Limit show - Paginate Prerequesit () |
|
20 | 20 | |
21 | - public $expose; # Returns all data (Uses get instead of paginate) |
|
21 | + public $expose; # Returns all data (Uses get instead of paginate) |
|
22 | 22 | |
23 | - public $debug; # For testing; |
|
23 | + public $debug; # For testing; |
|
24 | 24 | |
25 | - private $query; # Query builder |
|
25 | + private $query; # Query builder |
|
26 | 26 | |
27 | 27 | |
28 | 28 | public static function traversify($custom = NULL) |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | |
44 | 44 | $result = $this->__queryBuilder(); |
45 | 45 | |
46 | - if($this->debug && env('APP_DEBUG')): |
|
46 | + if ($this->debug && env('APP_DEBUG')): |
|
47 | 47 | |
48 | 48 | return \DB::getQueryLog(); |
49 | 49 | |
@@ -58,38 +58,36 @@ discard block |
||
58 | 58 | { |
59 | 59 | $this->query = $this->query(); |
60 | 60 | |
61 | - $queries = ['Search','Filter','Range', 'Order', 'Custom']; |
|
61 | + $queries = ['Search', 'Filter', 'Range', 'Order', 'Custom']; |
|
62 | 62 | |
63 | - self::loader($queries,'__query'); |
|
63 | + self::loader($queries, '__query'); |
|
64 | 64 | |
65 | 65 | return ($this->expose || $this->take) ? ['data' => $this->query->take($this->take)->get()] : $this->query->paginate($this->limit); |
66 | 66 | } |
67 | 67 | |
68 | 68 | private function __queryCustom() |
69 | 69 | { |
70 | - if(!isset($this->custom)) return; |
|
70 | + if (!isset($this->custom)) return; |
|
71 | 71 | |
72 | 72 | $this->query->where($this->custom); |
73 | 73 | } |
74 | 74 | |
75 | 75 | private function __querySearch() |
76 | 76 | { |
77 | - if(!isset(self::$searchables)) return; |
|
77 | + if (!isset(self::$searchables)) return; |
|
78 | 78 | |
79 | - $this->query->where( function($query) |
|
79 | + $this->query->where(function($query) |
|
80 | 80 | { |
81 | - foreach(self::$searchables ?: [] as $searchable): |
|
82 | - |
|
83 | - $_searchable = explode('~',$searchable); |
|
81 | + foreach (self::$searchables ?: [] as $searchable) : $_searchable = explode('~', $searchable); |
|
84 | 82 | |
85 | - if(count($_searchable)>1): |
|
83 | + if (count($_searchable) > 1): |
|
86 | 84 | |
87 | - $query->with($_searchable[0])->orWhereHas($_searchable[0], function($query) use ( $_searchable ) |
|
85 | + $query->with($_searchable[0])->orWhereHas($_searchable[0], function($query) use ($_searchable) |
|
88 | 86 | { |
89 | - $query->where($_searchable[1],'LIKE','%'.$this->search.'%'); |
|
87 | + $query->where($_searchable[1], 'LIKE', '%'.$this->search.'%'); |
|
90 | 88 | }); |
91 | 89 | else: |
92 | - $query->orWhere($searchable,'LIKE','%'.$this->search.'%'); |
|
90 | + $query->orWhere($searchable, 'LIKE', '%'.$this->search.'%'); |
|
93 | 91 | endif; |
94 | 92 | |
95 | 93 | endforeach; |
@@ -98,57 +96,55 @@ discard block |
||
98 | 96 | |
99 | 97 | private function __queryFilter() |
100 | 98 | { |
101 | - if(!isset(self::$filterables)) return; |
|
99 | + if (!isset(self::$filterables)) return; |
|
102 | 100 | |
103 | 101 | $this->query->where(function($query) |
104 | 102 | { |
105 | - foreach($this->filter ?: [] as $attribute => $value): |
|
106 | - |
|
107 | - $value == 'null' && $value = null; |
|
103 | + foreach ($this->filter ?: [] as $attribute => $value) : $value == 'null' && $value = null; |
|
108 | 104 | |
109 | - if(in_array($attribute, self::$filterables)): |
|
105 | + if (in_array($attribute, self::$filterables)): |
|
110 | 106 | |
111 | 107 | $relationship = explode('~', $attribute); |
112 | 108 | |
113 | - if(count($relationship) == 2): |
|
109 | + if (count($relationship) == 2): |
|
114 | 110 | |
115 | - if($value[0]=='!'): |
|
111 | + if ($value[0] == '!'): |
|
116 | 112 | |
117 | - $values = explode(',',substr($value,1)); |
|
113 | + $values = explode(',', substr($value, 1)); |
|
118 | 114 | |
119 | 115 | $value == 'null' && $value = null; |
120 | 116 | |
121 | - $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values) |
|
117 | + $query->with($relationship[0])->whereHas($relationship[0], function($query) use ($relationship, $values) |
|
122 | 118 | { |
123 | - $query->whereNotIn($relationship[1],$values); |
|
119 | + $query->whereNotIn($relationship[1], $values); |
|
124 | 120 | }); |
125 | 121 | |
126 | 122 | else: |
127 | 123 | |
128 | - $values = explode(',',$value); |
|
124 | + $values = explode(',', $value); |
|
129 | 125 | |
130 | - $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values, $value) |
|
126 | + $query->with($relationship[0])->whereHas($relationship[0], function($query) use ($relationship, $values, $value) |
|
131 | 127 | { |
132 | - count($values) > 1 ? $query->whereIn($relationship[1],$values) : $query->where($relationship[1],$value); |
|
128 | + count($values) > 1 ? $query->whereIn($relationship[1], $values) : $query->where($relationship[1], $value); |
|
133 | 129 | }); |
134 | 130 | |
135 | 131 | endif; |
136 | 132 | |
137 | 133 | else: |
138 | - if($value[0]=='!'): |
|
134 | + if ($value[0] == '!'): |
|
139 | 135 | |
140 | - $values = explode(',',substr($value,1)); |
|
136 | + $values = explode(',', substr($value, 1)); |
|
141 | 137 | |
142 | 138 | $query->where(function($query) use ($attribute, $values) |
143 | 139 | { |
144 | - $query->whereNotIn($attribute,$values); |
|
140 | + $query->whereNotIn($attribute, $values); |
|
145 | 141 | }); |
146 | 142 | |
147 | 143 | else: |
148 | 144 | |
149 | - $values = explode(',',$value); |
|
145 | + $values = explode(',', $value); |
|
150 | 146 | |
151 | - count($values) > 1 ? $query->whereIn($attribute,$values) : $query->where($attribute,$value); |
|
147 | + count($values) > 1 ? $query->whereIn($attribute, $values) : $query->where($attribute, $value); |
|
152 | 148 | |
153 | 149 | endif; |
154 | 150 | |
@@ -162,54 +158,52 @@ discard block |
||
162 | 158 | |
163 | 159 | private function __queryRange() |
164 | 160 | { |
165 | - if(!isset(self::$rangables)) return; |
|
161 | + if (!isset(self::$rangables)) return; |
|
166 | 162 | |
167 | 163 | $this->query->where(function($query) |
168 | 164 | { |
169 | - foreach($this->range ?: [] as $attribute => $value): |
|
170 | - |
|
171 | - if(in_array($attribute, self::$rangables)): |
|
165 | + foreach ($this->range ?: [] as $attribute => $value) : if (in_array($attribute, self::$rangables)): |
|
172 | 166 | |
173 | 167 | $relationship = explode('~', $attribute); |
174 | 168 | |
175 | - if(count($relationship) == 2): |
|
169 | + if (count($relationship) == 2): |
|
176 | 170 | |
177 | - if($value[0]=='!'): |
|
171 | + if ($value[0] == '!'): |
|
178 | 172 | |
179 | - $values = explode(',',substr($value,1)); |
|
173 | + $values = explode(',', substr($value, 1)); |
|
180 | 174 | |
181 | - $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values) |
|
175 | + $query->with($relationship[0])->whereHas($relationship[0], function($query) use ($relationship, $values) |
|
182 | 176 | { |
183 | - $query->whereNotBetween($relationship[1],$values); |
|
177 | + $query->whereNotBetween($relationship[1], $values); |
|
184 | 178 | }); |
185 | 179 | |
186 | 180 | else: |
187 | 181 | |
188 | - $values = explode(',',$value); |
|
182 | + $values = explode(',', $value); |
|
189 | 183 | |
190 | - $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values, $value) |
|
184 | + $query->with($relationship[0])->whereHas($relationship[0], function($query) use ($relationship, $values, $value) |
|
191 | 185 | { |
192 | - $query->whereBetween($relationship[1],$values); |
|
186 | + $query->whereBetween($relationship[1], $values); |
|
193 | 187 | }); |
194 | 188 | |
195 | 189 | endif; |
196 | 190 | |
197 | 191 | else: |
198 | 192 | |
199 | - if($value[0]=='!'): |
|
193 | + if ($value[0] == '!'): |
|
200 | 194 | |
201 | - $values = explode(',',substr($value,1)); |
|
195 | + $values = explode(',', substr($value, 1)); |
|
202 | 196 | |
203 | 197 | $query->where(function($q) use ($attribute, $values) |
204 | 198 | { |
205 | - $q->whereNotBetween($attribute,$values); |
|
199 | + $q->whereNotBetween($attribute, $values); |
|
206 | 200 | }); |
207 | 201 | |
208 | 202 | else: |
209 | 203 | |
210 | - $values = explode(',',$value); |
|
204 | + $values = explode(',', $value); |
|
211 | 205 | |
212 | - $query->whereBetween($attribute,$values); |
|
206 | + $query->whereBetween($attribute, $values); |
|
213 | 207 | |
214 | 208 | endif; |
215 | 209 | |
@@ -223,19 +217,17 @@ discard block |
||
223 | 217 | |
224 | 218 | private function __queryOrder() |
225 | 219 | { |
226 | - if(!isset(self::$orderables)) return; |
|
227 | - |
|
228 | - foreach($this->order ?: [] as $attribute => $value): |
|
220 | + if (!isset(self::$orderables)) return; |
|
229 | 221 | |
230 | - if(in_array($attribute, self::$orderables)): |
|
222 | + foreach ($this->order ?: [] as $attribute => $value) : if (in_array($attribute, self::$orderables)): |
|
231 | 223 | |
232 | 224 | $relationship = explode('~', $attribute); |
233 | 225 | |
234 | - if(count($relationship) == 2): |
|
226 | + if (count($relationship) == 2): |
|
235 | 227 | |
236 | - $this->query->with([$relationship[0] => function($query) use ( $relationship, $value) |
|
228 | + $this->query->with([$relationship[0] => function($query) use ($relationship, $value) |
|
237 | 229 | { |
238 | - $query->orderBy($relationship[1],$value); |
|
230 | + $query->orderBy($relationship[1], $value); |
|
239 | 231 | }]); |
240 | 232 | |
241 | 233 | else: |
@@ -249,16 +241,16 @@ discard block |
||
249 | 241 | endforeach; |
250 | 242 | } |
251 | 243 | |
252 | - private function __getters ( $getters = ['search', 'filter', 'take', 'order', 'range', 'limit', 'expose', 'debug'] ) |
|
244 | + private function __getters($getters = ['search', 'filter', 'take', 'order', 'range', 'limit', 'expose', 'debug']) |
|
253 | 245 | { |
254 | - foreach($getters as $getter): |
|
246 | + foreach ($getters as $getter): |
|
255 | 247 | !$this->$getter && $this->$getter = request()->get($getter); |
256 | 248 | endforeach; |
257 | 249 | } |
258 | 250 | |
259 | 251 | private function loader($loads, $prefix = '__') |
260 | 252 | { |
261 | - foreach($loads as $load): |
|
253 | + foreach ($loads as $load): |
|
262 | 254 | |
263 | 255 | self::{$prefix.$load}(); |
264 | 256 |
@@ -47,9 +47,11 @@ discard block |
||
47 | 47 | |
48 | 48 | return \DB::getQueryLog(); |
49 | 49 | |
50 | - else: |
|
50 | + else { |
|
51 | + : |
|
51 | 52 | |
52 | 53 | return $result; |
54 | + } |
|
53 | 55 | |
54 | 56 | endif; |
55 | 57 | } |
@@ -67,14 +69,18 @@ discard block |
||
67 | 69 | |
68 | 70 | private function __queryCustom() |
69 | 71 | { |
70 | - if(!isset($this->custom)) return; |
|
72 | + if(!isset($this->custom)) { |
|
73 | + return; |
|
74 | + } |
|
71 | 75 | |
72 | 76 | $this->query->where($this->custom); |
73 | 77 | } |
74 | 78 | |
75 | 79 | private function __querySearch() |
76 | 80 | { |
77 | - if(!isset(self::$searchables)) return; |
|
81 | + if(!isset(self::$searchables)) { |
|
82 | + return; |
|
83 | + } |
|
78 | 84 | |
79 | 85 | $this->query->where( function($query) |
80 | 86 | { |
@@ -88,8 +94,10 @@ discard block |
||
88 | 94 | { |
89 | 95 | $query->where($_searchable[1],'LIKE','%'.$this->search.'%'); |
90 | 96 | }); |
91 | - else: |
|
92 | - $query->orWhere($searchable,'LIKE','%'.$this->search.'%'); |
|
97 | + else { |
|
98 | + : |
|
99 | + $query->orWhere($searchable,'LIKE','%'.$this->search.'%'); |
|
100 | + } |
|
93 | 101 | endif; |
94 | 102 | |
95 | 103 | endforeach; |
@@ -98,7 +106,9 @@ discard block |
||
98 | 106 | |
99 | 107 | private function __queryFilter() |
100 | 108 | { |
101 | - if(!isset(self::$filterables)) return; |
|
109 | + if(!isset(self::$filterables)) { |
|
110 | + return; |
|
111 | + } |
|
102 | 112 | |
103 | 113 | $this->query->where(function($query) |
104 | 114 | { |
@@ -123,9 +133,11 @@ discard block |
||
123 | 133 | $query->whereNotIn($relationship[1],$values); |
124 | 134 | }); |
125 | 135 | |
126 | - else: |
|
136 | + else { |
|
137 | + : |
|
127 | 138 | |
128 | 139 | $values = explode(',',$value); |
140 | + } |
|
129 | 141 | |
130 | 142 | $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values, $value) |
131 | 143 | { |
@@ -134,19 +146,23 @@ discard block |
||
134 | 146 | |
135 | 147 | endif; |
136 | 148 | |
137 | - else: |
|
149 | + else { |
|
150 | + : |
|
138 | 151 | if($value[0]=='!'): |
139 | 152 | |
140 | 153 | $values = explode(',',substr($value,1)); |
154 | + } |
|
141 | 155 | |
142 | 156 | $query->where(function($query) use ($attribute, $values) |
143 | 157 | { |
144 | 158 | $query->whereNotIn($attribute,$values); |
145 | 159 | }); |
146 | 160 | |
147 | - else: |
|
161 | + else { |
|
162 | + : |
|
148 | 163 | |
149 | 164 | $values = explode(',',$value); |
165 | + } |
|
150 | 166 | |
151 | 167 | count($values) > 1 ? $query->whereIn($attribute,$values) : $query->where($attribute,$value); |
152 | 168 | |
@@ -162,7 +178,9 @@ discard block |
||
162 | 178 | |
163 | 179 | private function __queryRange() |
164 | 180 | { |
165 | - if(!isset(self::$rangables)) return; |
|
181 | + if(!isset(self::$rangables)) { |
|
182 | + return; |
|
183 | + } |
|
166 | 184 | |
167 | 185 | $this->query->where(function($query) |
168 | 186 | { |
@@ -183,9 +201,11 @@ discard block |
||
183 | 201 | $query->whereNotBetween($relationship[1],$values); |
184 | 202 | }); |
185 | 203 | |
186 | - else: |
|
204 | + else { |
|
205 | + : |
|
187 | 206 | |
188 | 207 | $values = explode(',',$value); |
208 | + } |
|
189 | 209 | |
190 | 210 | $query->with($relationship[0])->whereHas($relationship[0],function($query) use ( $relationship, $values, $value) |
191 | 211 | { |
@@ -194,20 +214,24 @@ discard block |
||
194 | 214 | |
195 | 215 | endif; |
196 | 216 | |
197 | - else: |
|
217 | + else { |
|
218 | + : |
|
198 | 219 | |
199 | 220 | if($value[0]=='!'): |
200 | 221 | |
201 | 222 | $values = explode(',',substr($value,1)); |
223 | + } |
|
202 | 224 | |
203 | 225 | $query->where(function($q) use ($attribute, $values) |
204 | 226 | { |
205 | 227 | $q->whereNotBetween($attribute,$values); |
206 | 228 | }); |
207 | 229 | |
208 | - else: |
|
230 | + else { |
|
231 | + : |
|
209 | 232 | |
210 | 233 | $values = explode(',',$value); |
234 | + } |
|
211 | 235 | |
212 | 236 | $query->whereBetween($attribute,$values); |
213 | 237 | |
@@ -223,7 +247,9 @@ discard block |
||
223 | 247 | |
224 | 248 | private function __queryOrder() |
225 | 249 | { |
226 | - if(!isset(self::$orderables)) return; |
|
250 | + if(!isset(self::$orderables)) { |
|
251 | + return; |
|
252 | + } |
|
227 | 253 | |
228 | 254 | foreach($this->order ?: [] as $attribute => $value): |
229 | 255 | |
@@ -238,9 +264,11 @@ discard block |
||
238 | 264 | $query->orderBy($relationship[1],$value); |
239 | 265 | }]); |
240 | 266 | |
241 | - else: |
|
267 | + else { |
|
268 | + : |
|
242 | 269 | |
243 | 270 | $this->query->orderBy($attribute, $value); |
271 | + } |
|
244 | 272 | |
245 | 273 | endif; |
246 | 274 |