1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Datatables\Engines; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Yajra\Datatables\Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EloquentEngine. |
11
|
|
|
* |
12
|
|
|
* @package Yajra\Datatables\Engines |
13
|
|
|
* @author Arjay Angeles <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class EloquentEngine extends QueryBuilderEngine |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Select trashed records in count function for models with soft deletes trait. |
19
|
|
|
* By default we do not select soft deleted records. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $withTrashed = false; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Select only trashed records in count function for models with soft deletes trait. |
27
|
|
|
* By default we do not select soft deleted records. |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $onlyTrashed = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $model |
35
|
|
|
* @param \Yajra\Datatables\Request $request |
36
|
|
|
*/ |
37
|
|
|
public function __construct($model, Request $request) |
38
|
|
|
{ |
39
|
|
|
$builder = $model instanceof Builder ? $model : $model->getQuery(); |
40
|
|
|
parent::__construct($builder->getQuery(), $request); |
41
|
|
|
|
42
|
|
|
$this->query = $builder; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Counts current query. |
47
|
|
|
* |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
public function count() |
51
|
|
|
{ |
52
|
|
|
$myQuery = clone $this->query; |
53
|
|
|
// if its a normal query ( no union, having and distinct word ) |
54
|
|
|
// replace the select with static text to improve performance |
55
|
|
View Code Duplication |
if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) { |
|
|
|
|
56
|
|
|
$row_count = $this->wrap('row_count'); |
57
|
|
|
$myQuery->select($this->connection->raw("'1' as {$row_count}")); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// check for select soft deleted records |
61
|
|
|
if (! $this->withTrashed && ! $this->onlyTrashed && $this->modelUseSoftDeletes()) { |
62
|
|
|
$myQuery->whereNull($myQuery->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->onlyTrashed && $this->modelUseSoftDeletes()) { |
66
|
|
|
$myQuery->whereNotNull($myQuery->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table')) |
70
|
|
|
->setBindings($myQuery->getBindings())->count(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if model use SoftDeletes trait. |
75
|
|
|
* |
76
|
|
|
* @return boolean |
77
|
|
|
*/ |
78
|
|
|
protected function modelUseSoftDeletes() |
79
|
|
|
{ |
80
|
|
|
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->query->getModel())); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Change withTrashed flag value. |
85
|
|
|
* |
86
|
|
|
* @param bool $withTrashed |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function withTrashed($withTrashed = true) |
90
|
|
|
{ |
91
|
|
|
$this->withTrashed = $withTrashed; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Change onlyTrashed flag value. |
98
|
|
|
* |
99
|
|
|
* @param bool $onlyTrashed |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function onlyTrashed($onlyTrashed = true) |
103
|
|
|
{ |
104
|
|
|
$this->onlyTrashed = $onlyTrashed; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* If column name could not be resolved then use primary key. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
protected function getPrimaryKeyName() |
115
|
|
|
{ |
116
|
|
|
return $this->query->getModel()->getKeyName(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: