Model.php
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 重写 laravel-admin 类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Rewrite\Grid
* @package App\Admin\Rewrite\Grid
* @author Richer <yangzi1028@163.com>
* @date 2021年9月7日09:33:10
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Rewrite\Grid;
use Encore\Admin\Grid\Model as Base;
use Illuminate\Support\Str;
/**
* Class Model
*
* @category App\Admin\Rewrite\Grid
* @package App\Admin\Rewrite\Grid
* @author Richer <yangzi1028@163.com>
* @date 2021年9月7日09:33:10
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class Model extends Base
{
/**
* Set the grid sort.
*
* @return void
*/
protected function setSort()
{
$this->sort = \request($this->sortName, []);
if (!is_array($this->sort)) {
return;
}
$columnName = $this->sort['column'] ?? null;
if ($columnName === null || empty($this->sort['type'])) {
return;
}
$columnNameContainsDots = Str::contains($columnName, '.');
$isRelation = $this->queries->contains(function ($query) use ($columnName) {
return $query['method'] === 'with' && in_array($columnName, $query['arguments'], true);
});
if ($columnNameContainsDots === true && $isRelation) {
$this->setRelationSort($columnName);
} else {
$this->resetOrderBy();
if ($columnNameContainsDots === true) {
//json
$this->resetOrderBy();
$explodedCols = explode('.', $this->sort['column']);
$col = array_shift($explodedCols);
$parts = implode('.', $explodedCols);
$columnName = "JSON_EXTRACT({$col}, '$.{$parts}')";
}
// get column. if contains "cast", set set column as cast
if (!empty($this->sort['cast'])) {
$column = "CAST({$columnName} AS {$this->sort['cast']}) {$this->sort['type']}";
$method = 'orderByRaw';
$arguments = [$column];
} elseif (!empty($this->sort['convert'])) {
// add by Richer 于 2021年9月7日09:34:33 增加中文字段排序规则
$column = "CONVERT({$columnName} USING gbk) {$this->sort['type']}";
$method = 'orderByRaw';
$arguments = [$column];
} else {
$column = $columnName;
$method = 'orderBy';
$arguments = [$column, $this->sort['type']];
}
$this->queries->push([
'method' => $method,
'arguments' => $arguments,
]);
}
}
}