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
|
|
|
* EloquentEngine constructor. |
35
|
|
|
* |
36
|
|
|
* @param mixed $model |
37
|
|
|
* @param \Yajra\Datatables\Request $request |
38
|
|
|
*/ |
39
|
|
|
public function __construct($model, Request $request) |
40
|
|
|
{ |
41
|
|
|
$builder = $model instanceof Builder ? $model : $model->getQuery(); |
42
|
|
|
parent::__construct($builder->getQuery(), $request); |
43
|
|
|
|
44
|
|
|
$this->query = $builder; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Counts current query. |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
|
|
public function count() |
53
|
|
|
{ |
54
|
|
|
$builder = clone $this->query; |
55
|
|
|
|
56
|
|
|
if ($this->isComplexQuery($builder)) { |
|
|
|
|
57
|
|
|
$row_count = $this->wrap('row_count'); |
58
|
|
|
$builder->select($this->connection->raw("'1' as {$row_count}")); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->isSoftDeleting()) { |
62
|
|
|
$builder->whereNull($builder->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->isOnlyTrashed()) { |
66
|
|
|
$builder->whereNotNull($builder->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->connection->table($this->connection->raw('(' . $builder->toSql() . ') count_row_table')) |
|
|
|
|
70
|
|
|
->setBindings($builder->getBindings())->count(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if builder query uses complex sql. |
75
|
|
|
* |
76
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
private function isComplexQuery($builder) |
80
|
|
|
{ |
81
|
|
|
return !Str::contains(Str::lower($builder->toSql()), ['union', 'having', 'distinct', 'order by', 'group by']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check if engine uses soft deletes. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
private function isSoftDeleting() |
90
|
|
|
{ |
91
|
|
|
return !$this->withTrashed && !$this->onlyTrashed && $this->modelUseSoftDeletes(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Check if model use SoftDeletes trait. |
96
|
|
|
* |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
private function modelUseSoftDeletes() |
100
|
|
|
{ |
101
|
|
|
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->query->getModel())); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check if engine uses only trashed. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
private function isOnlyTrashed() |
110
|
|
|
{ |
111
|
|
|
return $this->onlyTrashed && $this->modelUseSoftDeletes(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Change withTrashed flag value. |
116
|
|
|
* |
117
|
|
|
* @param bool $withTrashed |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function withTrashed($withTrashed = true) |
121
|
|
|
{ |
122
|
|
|
$this->withTrashed = $withTrashed; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Change onlyTrashed flag value. |
129
|
|
|
* |
130
|
|
|
* @param bool $onlyTrashed |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function onlyTrashed($onlyTrashed = true) |
134
|
|
|
{ |
135
|
|
|
$this->onlyTrashed = $onlyTrashed; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* If column name could not be resolved then use primary key. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
protected function getPrimaryKeyName() |
146
|
|
|
{ |
147
|
|
|
return $this->query->getModel()->getKeyName(); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.