|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eliurkis\Crud; |
|
4
|
|
|
|
|
5
|
|
|
use DB; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
|
|
8
|
|
|
trait CrudDataTable |
|
9
|
|
|
{ |
|
10
|
|
|
protected $dataTableActivated = true; |
|
11
|
|
|
|
|
12
|
|
|
public function index_source(Request $request) |
|
13
|
|
|
{ |
|
14
|
|
|
list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request); |
|
|
|
|
|
|
15
|
|
|
list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null); |
|
16
|
|
|
|
|
17
|
|
|
$query = $this->entity->orderBy($colSortBy, $colOrderBy); |
|
|
|
|
|
|
18
|
|
|
$query = $this->applySearchScope($query, $request->get('search')['value'] ?? null); |
|
19
|
|
|
|
|
20
|
|
|
$rows = $query->offset($request->get('start') ?? 0) |
|
21
|
|
|
->limit($request->get('length') ?? $totalRows) |
|
22
|
|
|
->get(); |
|
23
|
|
|
|
|
24
|
|
|
return response()->json([ |
|
25
|
|
|
'data' => $rows, |
|
26
|
|
|
'draw' => (int) ($request->get('draw') ?? 0), |
|
27
|
|
|
'recordsFiltered' => $totalRowsFiltered, |
|
28
|
|
|
'recordsTotal' => $totalRows, |
|
29
|
|
|
'colSortBy' => $colSortBy, |
|
30
|
|
|
'colOrderBy' => $colOrderBy, |
|
31
|
|
|
]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getSortInformation($cols, $request) |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
$cols[$request->get('order')[0]['column'] ?? 0], |
|
38
|
|
|
$request->get('order')[0]['dir'] ?? 'asc', |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getRowsTotals($searchValue = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$totalRows = $totalRowsFiltered = $this->getRowsTotal(); |
|
45
|
|
|
|
|
46
|
|
|
if ($searchValue != '' && $this->searchable) { |
|
|
|
|
|
|
47
|
|
|
$totalRowsFiltered = $this->getRowsTotal($searchValue); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return [$totalRows, $totalRowsFiltered]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getRowsTotal($searchValue = null) |
|
54
|
|
|
{ |
|
55
|
|
|
$query = $this->entity->select(DB::raw('count(*) as total')); |
|
56
|
|
|
$query = $this->applySearchScope($query, $searchValue); |
|
57
|
|
|
|
|
58
|
|
|
return $query->first() |
|
59
|
|
|
->total; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function applySearchScope($query, $searchValue = null) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($searchValue == '' || !$this->searchable) { |
|
65
|
|
|
return $query; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$searchable = $this->searchable; |
|
69
|
|
|
|
|
70
|
|
|
return $query->where(function ($query) use ($searchValue, $searchable) { |
|
71
|
|
|
foreach ($searchable as $key => $field) { |
|
72
|
|
|
$query = $key === 0 |
|
73
|
|
|
? $query->where($field, 'like', '%'.$searchValue.'%') |
|
74
|
|
|
: $query->orWhere($field, 'like', '%'.$searchValue.'%'); |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
return $query; |
|
78
|
|
|
}); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: