forked from underwear/laravel-vue-good-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformsSorting.php
39 lines (31 loc) · 964 Bytes
/
PerformsSorting.php
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
<?php
namespace LaravelVueGoodTable;
use Illuminate\Database\Query\Builder;
use LaravelVueGoodTable\Columns\Column;
use LaravelVueGoodTable\Contracts\Sortable;
use LaravelVueGoodTable\Http\Requests\DataRequest;
trait PerformsSorting
{
/**
* @param Column[] $columns
* @param Builder $queryBuilder
* @param DataRequest $request
*
* @return mixed
*/
protected function applySorting(array $columns, $queryBuilder, DataRequest $request)
{
$sort = $request->getSort();
if (empty($sort)) {
return $queryBuilder;
}
$sortField = $sort['field'];
$sortType = $sort['type'];
foreach ($columns as $column) {
if ($column->getAttribute() == $sortField and ($column instanceof Sortable) and $column->isSortable()) {
$queryBuilder = $column->sort($queryBuilder, $sortType);
}
}
return $queryBuilder;
}
}