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 indexDataTable(Request $request) |
13
|
|
|
{ |
14
|
|
|
if ($request->ajax() || $request->wantsJson()) { |
15
|
|
|
return $this->indexDataTableResults($request); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
return view('crud::list-datatable') |
19
|
|
|
->with('rows', []) |
20
|
|
|
->with('fields', $this->fields) |
|
|
|
|
21
|
|
|
->with('columns', $this->columns) |
|
|
|
|
22
|
|
|
->with('searchable', $this->searchable) |
|
|
|
|
23
|
|
|
->with('buttons', $this->buttons) |
|
|
|
|
24
|
|
|
->with('paginate', $this->paginate) |
|
|
|
|
25
|
|
|
->with('t', $this->texts) |
|
|
|
|
26
|
|
|
->with('htmlFilters', $this->htmlFilters) |
|
|
|
|
27
|
|
|
->with('listDisplay', $this->listDisplay) |
|
|
|
|
28
|
|
|
->with('request', $request) |
29
|
|
|
->with('orderBy', $this->orderBy) |
|
|
|
|
30
|
|
|
->with('route', $this->route); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function indexDataTableResults(Request $request) |
34
|
|
|
{ |
35
|
|
|
list($colSortBy, $colOrderBy) = $this->getSortInformation($this->columns, $request); |
36
|
|
|
list($totalRows, $totalRowsFiltered) = $this->getRowsTotals($request->get('search')['value'] ?? null); |
37
|
|
|
|
38
|
|
|
$query = $this->entity->orderBy($colSortBy, $colOrderBy); |
|
|
|
|
39
|
|
|
$query = $this->applySearchScope($query, $request->get('search')['value'] ?? null); |
40
|
|
|
|
41
|
|
|
$rows = $query->offset($request->get('start') ?? 0) |
42
|
|
|
->limit($request->get('length') ?? $totalRows) |
43
|
|
|
->get(); |
44
|
|
|
|
45
|
|
|
return response()->json([ |
46
|
|
|
'data' => $rows, |
47
|
|
|
'draw' => (int) ($request->get('draw') ?? 0), |
48
|
|
|
'recordsFiltered' => $totalRowsFiltered, |
49
|
|
|
'recordsTotal' => $totalRows, |
50
|
|
|
'colSortBy' => $colSortBy, |
51
|
|
|
'colOrderBy' => $colOrderBy, |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function getSortInformation($cols, $request) |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
$cols[$request->get('order')[0]['column'] ?? 0], |
59
|
|
|
$request->get('order')[0]['dir'] ?? 'asc', |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function getRowsTotals($searchValue = null) |
64
|
|
|
{ |
65
|
|
|
$totalRows = $totalRowsFiltered = $this->getRowsTotal(); |
66
|
|
|
|
67
|
|
|
if ($searchValue != '' && $this->searchable) { |
68
|
|
|
$totalRowsFiltered = $this->getRowsTotal($searchValue); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [$totalRows, $totalRowsFiltered]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getRowsTotal($searchValue = null) |
75
|
|
|
{ |
76
|
|
|
$query = $this->entity->select(DB::raw('count(*) as total')); |
77
|
|
|
$query = $this->applySearchScope($query, $searchValue); |
78
|
|
|
|
79
|
|
|
return $query->first() |
80
|
|
|
->total; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function applySearchScope($query, $searchValue = null) |
84
|
|
|
{ |
85
|
|
|
if ($searchValue == '' || !$this->searchable) { |
86
|
|
|
return $query; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$searchable = $this->searchable; |
90
|
|
|
|
91
|
|
|
return $query->where(function ($query) use ($searchValue, $searchable) { |
92
|
|
|
foreach ($searchable as $key => $field) { |
93
|
|
|
$query = $key === 0 |
94
|
|
|
? $query->where($field, 'like', '%'.$searchValue.'%') |
95
|
|
|
: $query->orWhere($field, 'like', '%'.$searchValue.'%'); |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
return $query; |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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: