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
|
|
|
$this->query_type = 'eloquent'; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Counts current query. |
48
|
|
|
* |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
public function count() |
52
|
|
|
{ |
53
|
|
|
$myQuery = clone $this->query; |
54
|
|
|
// if its a normal query ( no union, having and distinct word ) |
55
|
|
|
// replace the select with static text to improve performance |
56
|
|
View Code Duplication |
if (! Str::contains(Str::lower($myQuery->toSql()), ['union', 'having', 'distinct', 'order by', 'group by'])) { |
|
|
|
|
57
|
|
|
$row_count = $this->wrap('row_count'); |
58
|
|
|
$myQuery->select($this->connection->raw("'1' as {$row_count}")); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// check for select soft deleted records |
62
|
|
|
if (! $this->withTrashed && ! $this->onlyTrashed && $this->modelUseSoftDeletes()) { |
63
|
|
|
$myQuery->whereNull($myQuery->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->onlyTrashed && $this->modelUseSoftDeletes()) { |
67
|
|
|
$myQuery->whereNotNull($myQuery->getModel()->getQualifiedDeletedAtColumn()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->connection->table($this->connection->raw('(' . $myQuery->toSql() . ') count_row_table')) |
71
|
|
|
->setBindings($myQuery->getBindings())->count(); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if model use SoftDeletes trait |
76
|
|
|
* |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
|
|
protected function modelUseSoftDeletes() |
80
|
|
|
{ |
81
|
|
|
if ($this->query_type == 'eloquent') { |
82
|
|
|
return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->query->getModel())); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Change withTrashed flag value. |
90
|
|
|
* |
91
|
|
|
* @param bool $withTrashed |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function withTrashed($withTrashed = true) |
95
|
|
|
{ |
96
|
|
|
$this->withTrashed = $withTrashed; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Change onlyTrashed flag value. |
103
|
|
|
* |
104
|
|
|
* @param bool $onlyTrashed |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function onlyTrashed($onlyTrashed = true) |
108
|
|
|
{ |
109
|
|
|
$this->onlyTrashed = $onlyTrashed; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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: