1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOneOrMany; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
9
|
|
|
use Illuminate\Database\Query\Builder; |
10
|
|
|
use Illuminate\Support\Facades\Config; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use Yajra\Datatables\Helper; |
13
|
|
|
use Yajra\Datatables\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class QueryBuilderEngine. |
17
|
|
|
* |
18
|
|
|
* @package Yajra\Datatables\Engines |
19
|
|
|
* @author Arjay Angeles <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class QueryBuilderEngine extends BaseEngine |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
25
|
|
|
* @param \Yajra\Datatables\Request $request |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Builder $builder, Request $request) |
28
|
|
|
{ |
29
|
|
|
$this->query = $builder; |
30
|
|
|
$this->init($request, $builder); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initialize attributes. |
35
|
|
|
* |
36
|
|
|
* @param \Yajra\Datatables\Request $request |
37
|
|
|
* @param \Illuminate\Database\Query\Builder $builder |
38
|
|
|
* @param string $type |
39
|
|
|
*/ |
40
|
|
|
protected function init($request, $builder, $type = 'builder') |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$this->request = $request; |
43
|
|
|
$this->query_type = $type; |
44
|
|
|
$this->columns = $builder->columns; |
45
|
|
|
$this->connection = $builder->getConnection(); |
46
|
|
|
$this->prefix = $this->connection->getTablePrefix(); |
47
|
|
|
$this->database = $this->connection->getDriverName(); |
48
|
|
|
if ($this->isDebugging()) { |
49
|
|
|
$this->connection->enableQueryLog(); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set auto filter off and run your own filter. |
55
|
|
|
* Overrides global search |
56
|
|
|
* |
57
|
|
|
* @param \Closure $callback |
58
|
|
|
* @param bool $globalSearch |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function filter(Closure $callback, $globalSearch = false) |
62
|
|
|
{ |
63
|
|
|
$this->overrideGlobalSearch($callback, $this->query, $globalSearch); |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Organizes works |
70
|
|
|
* |
71
|
|
|
* @param bool $mDataSupport |
72
|
|
|
* @param bool $orderFirst |
73
|
|
|
* @return \Illuminate\Http\JsonResponse |
74
|
|
|
*/ |
75
|
|
|
public function make($mDataSupport = false, $orderFirst = false) |
76
|
|
|
{ |
77
|
|
|
return parent::make($mDataSupport, $orderFirst); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Count total items. |
82
|
|
|
* |
83
|
|
|
* @return integer |
84
|
|
|
*/ |
85
|
|
|
public function totalCount() |
86
|
|
|
{ |
87
|
|
|
return $this->totalRecords ? $this->totalRecords : $this->count(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Counts current query. |
92
|
|
|
* |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function count() |
96
|
|
|
{ |
97
|
|
|
$myQuery = clone $this->query; |
98
|
|
|
// if its a normal query ( no union, having and distinct word ) |
99
|
|
|
// replace the select with static text to improve performance |
100
|
|
|
if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) { |
|
|
|
|
101
|
|
|
$row_count = $this->wrap('row_count'); |
102
|
|
|
$myQuery->select($this->connection->raw("'1' as {$row_count}")); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// check for select soft deleted records |
106
|
|
|
if (! $this->withTrashed && $this->modelUseSoftDeletes()) { |
107
|
|
|
$myQuery->whereNull($myQuery->getModel()->getTable() . '.deleted_at'); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table')) |
111
|
|
|
->setBindings($myQuery->getBindings())->count(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Wrap column with DB grammar. |
116
|
|
|
* |
117
|
|
|
* @param string $column |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function wrap($column) |
121
|
|
|
{ |
122
|
|
|
return $this->connection->getQueryGrammar()->wrap($column); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if model use SoftDeletes trait |
127
|
|
|
* |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
|
private function modelUseSoftDeletes() |
131
|
|
|
{ |
132
|
|
|
if ($this->query_type == 'eloquent') { |
133
|
|
|
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->query->getModel())); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Perform global search. |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function filtering() |
145
|
|
|
{ |
146
|
|
|
$this->query->where( |
147
|
|
|
function ($query) { |
148
|
|
|
$globalKeyword = $this->request->keyword(); |
149
|
|
|
$queryBuilder = $this->getQueryBuilder($query); |
150
|
|
|
|
151
|
|
|
foreach ($this->request->searchableColumnIndex() as $index) { |
152
|
|
|
$columnName = $this->getColumnName($index); |
153
|
|
|
if ($this->isBlacklisted($columnName)) { |
154
|
|
|
continue; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// check if custom column filtering is applied |
158
|
|
|
if (isset($this->columnDef['filter'][$columnName])) { |
159
|
|
|
$columnDef = $this->columnDef['filter'][$columnName]; |
160
|
|
|
// check if global search should be applied for the specific column |
161
|
|
|
$applyGlobalSearch = count($columnDef['parameters']) == 0 || end($columnDef['parameters']) !== false; |
162
|
|
|
if (! $applyGlobalSearch) { |
163
|
|
|
continue; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
if ($columnDef['method'] instanceof Closure) { |
|
|
|
|
167
|
|
|
$whereQuery = $queryBuilder->newQuery(); |
168
|
|
|
call_user_func_array($columnDef['method'], [$whereQuery, $globalKeyword]); |
169
|
|
|
$queryBuilder->addNestedWhereQuery($whereQuery, 'or'); |
170
|
|
|
} else { |
171
|
|
|
$this->compileColumnQuery( |
172
|
|
|
$queryBuilder, |
173
|
|
|
Helper::getOrMethod($columnDef['method']), |
174
|
|
|
$columnDef['parameters'], |
175
|
|
|
$columnName, |
176
|
|
|
$globalKeyword |
|
|
|
|
177
|
|
|
); |
178
|
|
|
} |
179
|
|
View Code Duplication |
} else { |
|
|
|
|
180
|
|
|
if (count(explode('.', $columnName)) > 1) { |
181
|
|
|
$eagerLoads = $this->getEagerLoads(); |
182
|
|
|
$parts = explode('.', $columnName); |
183
|
|
|
$relationColumn = array_pop($parts); |
184
|
|
|
$relation = implode('.', $parts); |
185
|
|
|
if (in_array($relation, $eagerLoads)) { |
186
|
|
|
$this->compileRelationSearch( |
187
|
|
|
$queryBuilder, |
188
|
|
|
$relation, |
189
|
|
|
$relationColumn, |
190
|
|
|
$globalKeyword |
|
|
|
|
191
|
|
|
); |
192
|
|
|
} else { |
193
|
|
|
$this->compileQuerySearch($queryBuilder, $columnName, $globalKeyword); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$this->compileQuerySearch($queryBuilder, $columnName, $globalKeyword); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->isFilterApplied = true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Perform filter column on selected field. |
208
|
|
|
* |
209
|
|
|
* @param mixed $query |
210
|
|
|
* @param string|Closure $method |
211
|
|
|
* @param mixed $parameters |
212
|
|
|
* @param string $column |
213
|
|
|
* @param string $keyword |
214
|
|
|
*/ |
215
|
|
|
protected function compileColumnQuery($query, $method, $parameters, $column, $keyword) |
216
|
|
|
{ |
217
|
|
|
if (method_exists($query, $method) |
218
|
|
|
&& count($parameters) <= with(new \ReflectionMethod($query, $method))->getNumberOfParameters() |
219
|
|
|
) { |
220
|
|
|
if (Str::contains(Str::lower($method), 'raw') |
|
|
|
|
221
|
|
|
|| Str::contains(Str::lower($method), 'exists') |
|
|
|
|
222
|
|
|
) { |
223
|
|
|
call_user_func_array( |
224
|
|
|
[$query, $method], |
225
|
|
|
$this->parameterize($parameters, $keyword) |
226
|
|
|
); |
227
|
|
|
} else { |
228
|
|
|
call_user_func_array( |
229
|
|
|
[$query, $method], |
230
|
|
|
$this->parameterize($column, $parameters, $keyword) |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Build Query Builder Parameters. |
238
|
|
|
* |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
|
|
protected function parameterize() |
242
|
|
|
{ |
243
|
|
|
$args = func_get_args(); |
244
|
|
|
$keyword = count($args) > 2 ? $args[2] : $args[1]; |
245
|
|
|
$parameters = Helper::buildParameters($args); |
246
|
|
|
$parameters = Helper::replacePatternWithKeyword($parameters, $keyword, '$1'); |
247
|
|
|
|
248
|
|
|
return $parameters; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get eager loads keys if eloquent. |
253
|
|
|
* |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
|
protected function getEagerLoads() |
257
|
|
|
{ |
258
|
|
|
if ($this->query_type == 'eloquent') { |
259
|
|
|
return array_keys($this->query->getEagerLoads()); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return []; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Add relation query on global search. |
267
|
|
|
* |
268
|
|
|
* @param mixed $query |
269
|
|
|
* @param string $relation |
270
|
|
|
* @param string $column |
271
|
|
|
* @param string $keyword |
272
|
|
|
*/ |
273
|
|
|
protected function compileRelationSearch($query, $relation, $column, $keyword) |
274
|
|
|
{ |
275
|
|
|
$myQuery = clone $this->query; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* For compile nested relation, we need store all nested relation as array |
279
|
|
|
* and reverse order to apply where query. |
280
|
|
|
* With this method we can create nested sub query with properly relation. |
281
|
|
|
*/ |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Store all relation data that require in next step |
285
|
|
|
*/ |
286
|
|
|
$relationChunk = []; |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Store last eloquent query builder for get next relation. |
290
|
|
|
*/ |
291
|
|
|
$lastQuery = $query; |
292
|
|
|
|
293
|
|
|
$relations = explode('.', $relation); |
294
|
|
|
$lastRelation = end($relations); |
295
|
|
|
foreach ($relations as $relation) { |
296
|
|
|
$relationType = $myQuery->getModel()->{$relation}(); |
297
|
|
|
$myQuery->orWhereHas($relation, function ($builder) use ( |
298
|
|
|
$column, |
299
|
|
|
$keyword, |
300
|
|
|
$query, |
301
|
|
|
$relationType, |
302
|
|
|
$relation, |
303
|
|
|
$lastRelation, |
304
|
|
|
&$relationChunk, |
305
|
|
|
&$lastQuery |
306
|
|
|
) { |
307
|
|
|
$builder->select($this->connection->raw('count(1)')); |
308
|
|
|
|
309
|
|
|
// We will perform search on last relation only. |
310
|
|
|
if ($relation == $lastRelation) { |
311
|
|
|
$this->compileQuerySearch($builder, $column, $keyword, ''); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// Put require object to next step!! |
315
|
|
|
$relationChunk[$relation] = [ |
316
|
|
|
'builder' => $builder, |
317
|
|
|
'relationType' => $relationType, |
318
|
|
|
'query' => $lastQuery, |
319
|
|
|
]; |
320
|
|
|
|
321
|
|
|
// This is trick make sub query. |
322
|
|
|
$lastQuery = $builder; |
323
|
|
|
}); |
324
|
|
|
|
325
|
|
|
// This is trick to make nested relation by pass previous relation to be next query eloquent builder |
326
|
|
|
$myQuery = $relationType; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Reverse them all |
331
|
|
|
*/ |
332
|
|
|
$relationChunk = array_reverse($relationChunk, true); |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Create valuable for use in check last relation |
336
|
|
|
*/ |
337
|
|
|
end($relationChunk); |
338
|
|
|
$lastRelation = key($relationChunk); |
339
|
|
|
reset($relationChunk); |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Walking ... |
343
|
|
|
*/ |
344
|
|
|
foreach ($relationChunk as $relation => $chunk) { |
345
|
|
|
// Prepare variables |
346
|
|
|
$builder = $chunk['builder']; |
347
|
|
|
$relationType = $chunk['relationType']; |
348
|
|
|
$query = $chunk['query']; |
349
|
|
|
$builder = "({$builder->toSql()}) >= 1"; |
350
|
|
|
|
351
|
|
|
// Check if it last relation we will use orWhereRaw |
352
|
|
|
if ($lastRelation == $relation) { |
353
|
|
|
$relationMethod = "orWhereRaw"; |
354
|
|
|
} else { |
355
|
|
|
// For case parent relation of nested relation. |
356
|
|
|
// We must use and for properly query and get correct result |
357
|
|
|
$relationMethod = "whereRaw"; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if ($relationType instanceof MorphToMany) { |
361
|
|
|
$query->{$relationMethod}($builder, [$relationType->getMorphClass(), $this->prepareKeyword($keyword)]); |
362
|
|
|
} else { |
363
|
|
|
$query->{$relationMethod}($builder, [$this->prepareKeyword($keyword)]); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Compile query builder where clause depending on configurations. |
370
|
|
|
* |
371
|
|
|
* @param mixed $query |
372
|
|
|
* @param string $column |
373
|
|
|
* @param string $keyword |
374
|
|
|
* @param string $relation |
375
|
|
|
*/ |
376
|
|
|
protected function compileQuerySearch($query, $column, $keyword, $relation = 'or') |
377
|
|
|
{ |
378
|
|
|
$column = $this->castColumn($column); |
379
|
|
|
$sql = $column . ' LIKE ?'; |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Patch for fix about ambiguous field |
383
|
|
|
* Ambiguous field error will appear |
384
|
|
|
* when query use join table and search with keyword. |
385
|
|
|
*/ |
386
|
|
|
// Remove delimiter of column that appear from MYSQL query. |
387
|
|
|
$column = str_replace(['`', '"', '[', ']'], '', $column); |
388
|
|
|
|
389
|
|
|
// check . in field name for protect don't add table again |
390
|
|
|
// but as far as I tested, this function has single field name only. |
391
|
|
|
if (strpos($column, '.') === false) { |
392
|
|
|
// Alternative method to check |
393
|
|
|
// instanceof \Illuminate\Database\Eloquent\Builder |
394
|
|
|
if (method_exists($query, 'getQuery')) { |
395
|
|
|
$q = $query->getQuery(); |
396
|
|
|
} else { |
397
|
|
|
$q = $query; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// get table from query and add it. |
401
|
|
|
$column = $q->from . '.' . $column; |
402
|
|
|
} |
403
|
|
|
// Add wrap cover table and field name. |
404
|
|
|
$column = $this->wrap($column); |
405
|
|
|
|
406
|
|
|
/* end fix */ |
407
|
|
|
|
408
|
|
|
if ($this->isCaseInsensitive()) { |
409
|
|
|
$sql = 'LOWER(' . $column . ') LIKE ?'; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
$query->{$relation . 'WhereRaw'}($sql, [$this->prepareKeyword($keyword)]); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* Wrap a column and cast in pgsql. |
417
|
|
|
* |
418
|
|
|
* @param string $column |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
|
|
public function castColumn($column) |
422
|
|
|
{ |
423
|
|
|
$column = $this->wrap($column); |
424
|
|
|
if ($this->database === 'pgsql') { |
425
|
|
|
$column = 'CAST(' . $column . ' as TEXT)'; |
426
|
|
|
} elseif ($this->database === 'firebird') { |
427
|
|
|
$column = 'CAST(' . $column . ' as VARCHAR(255))'; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $column; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Prepare search keyword based on configurations. |
435
|
|
|
* |
436
|
|
|
* @param string $keyword |
437
|
|
|
* @return string |
438
|
|
|
*/ |
439
|
|
|
protected function prepareKeyword($keyword) |
440
|
|
|
{ |
441
|
|
|
if ($this->isCaseInsensitive()) { |
442
|
|
|
$keyword = Str::lower($keyword); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
if ($this->isWildcard()) { |
446
|
|
|
$keyword = $this->wildcardLikeString($keyword); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
if ($this->isSmartSearch()) { |
450
|
|
|
$keyword = "%$keyword%"; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
return $keyword; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* Perform column search. |
458
|
|
|
* |
459
|
|
|
* @return void |
460
|
|
|
*/ |
461
|
|
|
public function columnSearch() |
462
|
|
|
{ |
463
|
|
|
$columns = (array) $this->request->input('columns'); |
464
|
|
|
|
465
|
|
|
foreach ($columns as $index => $column) { |
466
|
|
|
if (! $this->request->isColumnSearchable($index)) { |
467
|
|
|
continue; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
$column = $this->getColumnName($index); |
471
|
|
|
|
472
|
|
|
if (isset($this->columnDef['filter'][$column])) { |
473
|
|
|
$columnDef = $this->columnDef['filter'][$column]; |
474
|
|
|
// get a raw keyword (without wildcards) |
475
|
|
|
$keyword = $this->getSearchKeyword($index, true); |
476
|
|
|
$builder = $this->getQueryBuilder(); |
477
|
|
|
|
478
|
|
View Code Duplication |
if ($columnDef['method'] instanceof Closure) { |
|
|
|
|
479
|
|
|
$whereQuery = $builder->newQuery(); |
480
|
|
|
call_user_func_array($columnDef['method'], [$whereQuery, $keyword]); |
481
|
|
|
$builder->addNestedWhereQuery($whereQuery); |
482
|
|
|
} else { |
483
|
|
|
$this->compileColumnQuery( |
484
|
|
|
$builder, |
485
|
|
|
$columnDef['method'], |
486
|
|
|
$columnDef['parameters'], |
487
|
|
|
$column, |
488
|
|
|
$keyword |
|
|
|
|
489
|
|
|
); |
490
|
|
|
} |
491
|
|
View Code Duplication |
} else { |
|
|
|
|
492
|
|
|
if (count(explode('.', $column)) > 1) { |
493
|
|
|
$eagerLoads = $this->getEagerLoads(); |
494
|
|
|
$parts = explode('.', $column); |
495
|
|
|
$relationColumn = array_pop($parts); |
496
|
|
|
$relation = implode('.', $parts); |
497
|
|
|
if (in_array($relation, $eagerLoads)) { |
498
|
|
|
$column = $this->joinEagerLoadedColumn($relation, $relationColumn); |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
$keyword = $this->getSearchKeyword($index); |
503
|
|
|
$this->compileColumnSearch($index, $column, $keyword); |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$this->isFilterApplied = true; |
507
|
|
|
} |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
/** |
511
|
|
|
* Get proper keyword to use for search. |
512
|
|
|
* |
513
|
|
|
* @param int $i |
514
|
|
|
* @param bool $raw |
515
|
|
|
* @return string |
516
|
|
|
*/ |
517
|
|
|
private function getSearchKeyword($i, $raw = false) |
518
|
|
|
{ |
519
|
|
|
$keyword = $this->request->columnKeyword($i); |
520
|
|
|
if ($raw || $this->request->isRegex($i)) { |
521
|
|
|
return $keyword; |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
return $this->setupKeyword($keyword); |
|
|
|
|
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Join eager loaded relation and get the related column name. |
529
|
|
|
* |
530
|
|
|
* @param string $relation |
531
|
|
|
* @param string $relationColumn |
532
|
|
|
* @return string |
533
|
|
|
*/ |
534
|
|
|
protected function joinEagerLoadedColumn($relation, $relationColumn) |
535
|
|
|
{ |
536
|
|
|
$joins = []; |
537
|
|
|
foreach ((array) $this->getQueryBuilder()->joins as $key => $join) { |
538
|
|
|
$joins[] = $join->table; |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
$model = $this->query->getRelation($relation); |
|
|
|
|
542
|
|
|
if ($model instanceof BelongsToMany) { |
543
|
|
|
$pivot = $model->getTable(); |
544
|
|
|
$pivotPK = $model->getForeignKey(); |
545
|
|
|
$pivotFK = $model->getQualifiedParentKeyName(); |
546
|
|
|
|
547
|
|
|
if (! in_array($pivot, $joins)) { |
548
|
|
|
$this->getQueryBuilder()->leftJoin($pivot, $pivotPK, '=', $pivotFK); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
$related = $model->getRelated(); |
552
|
|
|
$table = $related->getTable(); |
553
|
|
|
$tablePK = $related->getForeignKey(); |
554
|
|
|
$tableFK = $related->getQualifiedKeyName(); |
555
|
|
|
|
556
|
|
|
if (! in_array($table, $joins)) { |
557
|
|
|
$this->getQueryBuilder()->leftJoin($table, $pivot . '.' . $tablePK, '=', $tableFK); |
558
|
|
|
} |
559
|
|
|
} else { |
560
|
|
|
$table = $model->getRelated()->getTable(); |
561
|
|
|
if ($model instanceof HasOneOrMany) { |
562
|
|
|
$foreign = $model->getForeignKey(); |
563
|
|
|
$other = $model->getQualifiedParentKeyName(); |
564
|
|
|
} else { |
565
|
|
|
$foreign = $model->getQualifiedForeignKey(); |
566
|
|
|
$other = $model->getQualifiedOtherKeyName(); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
if (! in_array($table, $joins)) { |
570
|
|
|
$this->getQueryBuilder()->leftJoin($table, $foreign, '=', $other); |
571
|
|
|
} |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
$column = $table . '.' . $relationColumn; |
575
|
|
|
|
576
|
|
|
return $column; |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
/** |
580
|
|
|
* Compile queries for column search. |
581
|
|
|
* |
582
|
|
|
* @param int $i |
583
|
|
|
* @param mixed $column |
584
|
|
|
* @param string $keyword |
585
|
|
|
*/ |
586
|
|
|
protected function compileColumnSearch($i, $column, $keyword) |
587
|
|
|
{ |
588
|
|
|
if ($this->request->isRegex($i)) { |
589
|
|
|
$column = strstr($column, '(') ? $this->connection->raw($column) : $column; |
590
|
|
|
$this->regexColumnSearch($column, $keyword); |
591
|
|
|
} else { |
592
|
|
|
$this->compileQuerySearch($this->query, $column, $keyword, ''); |
593
|
|
|
} |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Compile regex query column search. |
598
|
|
|
* |
599
|
|
|
* @param mixed $column |
600
|
|
|
* @param string $keyword |
601
|
|
|
*/ |
602
|
|
|
protected function regexColumnSearch($column, $keyword) |
603
|
|
|
{ |
604
|
|
|
if ($this->isOracleSql()) { |
605
|
|
|
$sql = ! $this->isCaseInsensitive() ? 'REGEXP_LIKE( ' . $column . ' , ? )' : 'REGEXP_LIKE( LOWER(' . $column . ') , ?, \'i\' )'; |
606
|
|
|
$this->query->whereRaw($sql, [$keyword]); |
|
|
|
|
607
|
|
|
} else { |
608
|
|
|
$sql = ! $this->isCaseInsensitive() ? $column . ' REGEXP ?' : 'LOWER(' . $column . ') REGEXP ?'; |
609
|
|
|
$this->query->whereRaw($sql, [Str::lower($keyword)]); |
610
|
|
|
} |
611
|
|
|
} |
612
|
|
|
|
613
|
|
|
/** |
614
|
|
|
* Perform sorting of columns. |
615
|
|
|
* |
616
|
|
|
* @return void |
617
|
|
|
*/ |
618
|
|
|
public function ordering() |
619
|
|
|
{ |
620
|
|
|
if ($this->orderCallback) { |
621
|
|
|
call_user_func($this->orderCallback, $this->getQueryBuilder()); |
622
|
|
|
|
623
|
|
|
return; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
foreach ($this->request->orderableColumns() as $orderable) { |
627
|
|
|
$column = $this->getColumnName($orderable['column'], true); |
628
|
|
|
|
629
|
|
|
if ($this->isBlacklisted($column)) { |
630
|
|
|
continue; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
if (isset($this->columnDef['order'][$column])) { |
634
|
|
|
$method = $this->columnDef['order'][$column]['method']; |
635
|
|
|
$parameters = $this->columnDef['order'][$column]['parameters']; |
636
|
|
|
$this->compileColumnQuery( |
637
|
|
|
$this->getQueryBuilder(), |
638
|
|
|
$method, |
639
|
|
|
$parameters, |
640
|
|
|
$column, |
641
|
|
|
$orderable['direction'] |
642
|
|
|
); |
643
|
|
|
} else { |
644
|
|
|
$valid = 1; |
645
|
|
|
if (count(explode('.', $column)) > 1) { |
646
|
|
|
$eagerLoads = $this->getEagerLoads(); |
647
|
|
|
$parts = explode('.', $column); |
648
|
|
|
$relationColumn = array_pop($parts); |
649
|
|
|
$relation = implode('.', $parts); |
650
|
|
|
|
651
|
|
|
if (in_array($relation, $eagerLoads)) { |
652
|
|
|
$relationship = $this->query->getRelation($relation); |
|
|
|
|
653
|
|
|
if (! ($relationship instanceof MorphToMany)) { |
654
|
|
|
$column = $this->joinEagerLoadedColumn($relation, $relationColumn); |
655
|
|
|
} else { |
656
|
|
|
$valid = 0; |
657
|
|
|
} |
658
|
|
|
} |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
if ($valid == 1) { |
662
|
|
|
if ($this->nullsLast) { |
663
|
|
|
$this->getQueryBuilder()->orderByRaw($this->getNullsLastSql($column, $orderable['direction'])); |
664
|
|
|
} else { |
665
|
|
|
$this->getQueryBuilder()->orderBy($column, $orderable['direction']); |
666
|
|
|
} |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* Get NULLS LAST SQL. |
674
|
|
|
* |
675
|
|
|
* @param string $column |
676
|
|
|
* @param string $direction |
677
|
|
|
* @return string |
678
|
|
|
*/ |
679
|
|
|
protected function getNullsLastSql($column, $direction) |
680
|
|
|
{ |
681
|
|
|
$sql = Config::get('datatables.nulls_last_sql', '%s %s NULLS LAST'); |
682
|
|
|
|
683
|
|
|
return sprintf($sql, $column, $direction); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* Perform pagination |
688
|
|
|
* |
689
|
|
|
* @return void |
690
|
|
|
*/ |
691
|
|
|
public function paging() |
692
|
|
|
{ |
693
|
|
|
$this->query->skip($this->request->input('start')) |
|
|
|
|
694
|
|
|
->take((int) $this->request->input('length') > 0 ? $this->request->input('length') : 10); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Get results |
699
|
|
|
* |
700
|
|
|
* @return array|static[] |
701
|
|
|
*/ |
702
|
|
|
public function results() |
703
|
|
|
{ |
704
|
|
|
return $this->query->get(); |
705
|
|
|
} |
706
|
|
|
} |
707
|
|
|
|