|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Grid\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Grid\Column; |
|
6
|
|
|
use Illuminate\Database\Query\Builder; |
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
class TotalRow extends AbstractTool |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Builder |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $query; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $columns; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Collection |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $visibleColumns; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* TotalRow constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Builder $query |
|
31
|
|
|
* @param array $columns |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($query, array $columns) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->query = $query; |
|
36
|
|
|
|
|
37
|
|
|
$this->columns = $columns; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get total value of current column. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $column |
|
44
|
|
|
* @param mixed $display |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function total($column, $display = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!is_callable($display) && !is_null($display)) { |
|
51
|
|
|
return $display; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$sum = $this->query->sum($column); |
|
55
|
|
|
|
|
56
|
|
|
if (is_callable($display)) { |
|
57
|
|
|
return call_user_func($display, $sum); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $sum; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Collection $columns |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setVisibleColumns($columns) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->visibleColumns = $columns; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return Collection|static |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getVisibleColumns() |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->visibleColumns) { |
|
77
|
|
|
return $this->visibleColumns; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->getGrid()->visibleColumns(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Render total-row. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
87
|
|
|
*/ |
|
88
|
|
|
public function render() |
|
89
|
|
|
{ |
|
90
|
|
|
$columns = $this->getVisibleColumns()->map(function (Column $column) { |
|
|
|
|
|
|
91
|
|
|
$name = $column->getName(); |
|
92
|
|
|
|
|
93
|
|
|
$total = ''; |
|
94
|
|
|
|
|
95
|
|
|
if (Arr::has($this->columns, $name)) { |
|
96
|
|
|
$total = $this->total($name, Arr::get($this->columns, $name)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return [ |
|
100
|
|
|
'class' => $column->getClassName(), |
|
101
|
|
|
'value' => $total |
|
102
|
|
|
]; |
|
103
|
|
|
}); |
|
104
|
|
|
|
|
105
|
|
|
return view('admin::grid.total-row', compact('columns')); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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: