1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Boduch\Grid\Source; |
4
|
|
|
|
5
|
|
|
use Boduch\Grid\Column; |
6
|
|
|
use Boduch\Grid\Filters\FilterOperator; |
7
|
|
|
use Boduch\Grid\Order; |
8
|
|
|
|
9
|
|
|
class EloquentDataSource implements SourceInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Illuminate\Database\Eloquent\Builder |
13
|
|
|
*/ |
14
|
|
|
protected $source; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $source |
18
|
|
|
*/ |
19
|
|
|
public function __construct($source) |
20
|
|
|
{ |
21
|
|
|
$this->source = $source; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Column[] $columns |
26
|
|
|
*/ |
27
|
|
|
public function applyFilters($columns) |
28
|
|
|
{ |
29
|
|
|
foreach ($columns as $column) { |
30
|
|
|
/** @var \Boduch\Grid\Column $column */ |
31
|
|
|
if ($column->isFilterable() && !$column->getFilter()->isEmpty()) { |
32
|
|
|
$this->buildQuery($column); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $perPage |
39
|
|
|
* @param int $currentPage |
40
|
|
|
* @param Order $order |
41
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
42
|
|
|
*/ |
43
|
|
|
public function execute($perPage, $currentPage, Order $order) |
44
|
|
|
{ |
45
|
|
|
return $this |
46
|
|
|
->source |
47
|
|
|
->when($order->getColumn(), function ($builder) use ($order) { |
|
|
|
|
48
|
|
|
return $builder->orderBy($order->getColumn(), $order->getDirection()); |
49
|
|
|
}) |
50
|
|
|
->forPage($currentPage, $perPage) |
51
|
|
|
->get(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
|
|
public function total() |
58
|
|
|
{ |
59
|
|
|
return $this->source->count(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Column $column |
64
|
|
|
*/ |
65
|
|
|
protected function buildQuery(Column $column) |
66
|
|
|
{ |
67
|
|
|
$name = $column->getFilter()->getName(); |
68
|
|
|
$input = $column->getFilter()->getInput(); |
69
|
|
|
|
70
|
|
|
if ($column->getFilter()->getOperator() == FilterOperator::OPERATOR_BETWEEN) { |
71
|
|
|
$this->source->whereBetween($name, $input); |
72
|
|
|
} else { |
73
|
|
|
$this->source->where( |
74
|
|
|
$name, |
75
|
|
|
$column->getFilter()->getOperator(), |
76
|
|
|
$this->normalizeValue($input, $column->getFilter()->getOperator()) |
|
|
|
|
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string|string[] $value |
83
|
|
|
* @param string $operator |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
protected function normalizeValue($value, $operator) |
87
|
|
|
{ |
88
|
|
|
if (is_array($value)) { |
89
|
|
|
$value = array_filter($value); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($operator == FilterOperator::OPERATOR_LIKE || $operator == FilterOperator::OPERATOR_ILIKE) { |
93
|
|
|
$value = str_replace('*', '%', $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $value; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: