|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Microboard\DataTables; |
|
4
|
|
|
|
|
5
|
|
|
use App\User; |
|
6
|
|
|
use Yajra\DataTables\DataTableAbstract; |
|
7
|
|
|
use Yajra\DataTables\Html\Builder; |
|
8
|
|
|
use Yajra\DataTables\Html\Button; |
|
9
|
|
|
use Yajra\DataTables\Html\Column; |
|
10
|
|
|
use Yajra\DataTables\Services\DataTable; |
|
11
|
|
|
|
|
12
|
|
|
class UserDataTable extends DataTable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Build DataTable class. |
|
16
|
|
|
* |
|
17
|
|
|
* @param mixed $query Results from query() method. |
|
18
|
|
|
* @return DataTableAbstract |
|
19
|
|
|
*/ |
|
20
|
|
|
public function dataTable($query) |
|
21
|
|
|
{ |
|
22
|
|
|
return datatables() |
|
|
|
|
|
|
23
|
|
|
->eloquent($query) |
|
24
|
|
|
->editColumn('role_id', function (User $user) { |
|
25
|
|
|
if (auth()->user()->can('view', $user->role)) { |
|
|
|
|
|
|
26
|
|
|
$route = route('microboard.roles.show', $user->role); |
|
27
|
|
|
return "<a href='{$route}'>{$user->role->display_name}</a>"; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $user->role->display_name; |
|
31
|
|
|
}) |
|
32
|
|
|
->editColumn('name', function (User $user) { |
|
33
|
|
|
return '<div class="media align-items-center">' . |
|
34
|
|
|
'<span class="avatar avatar-sm rounded-circle mr-3">' . |
|
35
|
|
|
'<img alt="' . $user->name . '" src="' . $user->avatar . '"></span>' . |
|
36
|
|
|
'<div class="media-body">' . |
|
37
|
|
|
'<span class="mb-0 d-block">' . $user->name . '</span>' . |
|
38
|
|
|
'<small class="mb-0" style="font-size: 10px;"><i class="fa fa-clock"></i> ' . |
|
39
|
|
|
'<time datetime="'. $user->updated_at .'">' . |
|
40
|
|
|
trans('microboard::users.fields.updated_at') . ' ' . |
|
41
|
|
|
$user->updated_at->diffForHumans() . '</time></small>' . |
|
42
|
|
|
'</div></div>'; |
|
43
|
|
|
}) |
|
44
|
|
|
->editColumn('created_at', function(User $user) { |
|
45
|
|
|
return "<time datetime='{$user->created_at}'>{$user->created_at->format('d/m/Y')}</time>"; |
|
46
|
|
|
}) |
|
47
|
|
|
->addColumn('action', function (User $user) { |
|
48
|
|
|
$html = ''; |
|
49
|
|
|
|
|
50
|
|
|
if (auth()->user()->can('view', $user)) { |
|
|
|
|
|
|
51
|
|
|
$html .= '<a href="' . route('microboard.users.show', $user) . '" ' . |
|
52
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
|
53
|
|
|
'data-original-title="' . trans('microboard::users.view.action-button') . '">' . |
|
54
|
|
|
'<i class="fas fa-eye"></i></a>'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (auth()->user()->can('update', $user)) { |
|
58
|
|
|
$html .= '<a href="' . route('microboard.users.edit', $user) . '" ' . |
|
59
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
|
60
|
|
|
'data-original-title="' . trans('microboard::users.edit.action-button') . '">' . |
|
61
|
|
|
'<i class="fas fa-edit"></i></a>'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (auth()->user()->can('delete', $user)) { |
|
65
|
|
|
$html .= '<form action="' . route('microboard.users.destroy', $user) . '" method="post" class="d-inline-block">' . |
|
66
|
|
|
csrf_field() . method_field('DELETE') . |
|
67
|
|
|
'<button type="submit" data-toggle="tooltip" ' . |
|
68
|
|
|
'class="bg-transparent border-0 p-0 table-action table-action-delete" ' . |
|
69
|
|
|
'data-original-title="' . trans('microboard::users.delete.action-button') . '" ' . |
|
70
|
|
|
'data-modal-title="' . trans('microboard::users.delete.title') . '" ' . |
|
71
|
|
|
'data-modal-text="' . trans('microboard::users.delete.text') . '" ' . |
|
72
|
|
|
'data-confirm="' . trans('microboard::users.delete.confirm') . '" ' . |
|
73
|
|
|
'data-cancel="' . trans('microboard::users.delete.cancel') . '">' . |
|
74
|
|
|
'<i class="fas fa-trash"></i></button></form>'; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $html; |
|
78
|
|
|
}) |
|
79
|
|
|
->escapeColumns(['*']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get query source of dataTable. |
|
84
|
|
|
* |
|
85
|
|
|
* @param User $model |
|
86
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
87
|
|
|
*/ |
|
88
|
|
|
public function query(User $model) |
|
89
|
|
|
{ |
|
90
|
|
|
return $model->newQuery()->with(['role']); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Optional method if you want to use html builder. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Builder |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function html() |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
return $this->builder() |
|
101
|
|
|
->language(trans('microboard::datatable', [])) |
|
102
|
|
|
->addTableClass('table table-striped table-hover table-sm align-items-center') |
|
103
|
|
|
->columns($this->getColumns()) |
|
104
|
|
|
->setTableId('user-table') |
|
105
|
|
|
->autoWidth(false) |
|
106
|
|
|
->orderBy(0) |
|
107
|
|
|
->minifiedAjax() |
|
108
|
|
|
->dom("<'card'" . |
|
109
|
|
|
"<'card-header border-0'<'row align-items-center'<'col-12 col-sm-8 col-md-6'<'row no-gutters'<'col-4'l><'col-8'f>>><'col-12 col-sm-4 col-md-6 text-right'B>>>" . |
|
110
|
|
|
"<'table-responsive't>" . |
|
111
|
|
|
"<'card-footer'<'row'<'col-12 col-sm-6'i><'col-12 col-sm-6'p>>>" . |
|
112
|
|
|
"r>") |
|
113
|
|
|
->buttons( |
|
114
|
|
|
Button::make('print')->text('<span>' . trans('microboard::datatable.print') . '</span><i class="fa fa-print"></i>'), |
|
115
|
|
|
Button::make('excel')->text('<span>' . trans('microboard::datatable.excel') . '</span><i class="fa fa-file-excel"></i>'), |
|
116
|
|
|
Button::make('reload')->text('<span>' . trans('microboard::datatable.reload') . '</span><i class="fa fa-sync"></i>') |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get columns. |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getColumns() |
|
126
|
|
|
{ |
|
127
|
|
|
return [ |
|
128
|
|
|
Column::make('id')->title(trans('microboard::users.fields.id'))->width('1%'), |
|
129
|
|
|
Column::make('name')->title(trans('microboard::users.fields.name'))->width('25%'), |
|
130
|
|
|
Column::make('role_id')->title(trans('microboard::users.fields.role_id')), |
|
131
|
|
|
Column::make('email')->title(trans('microboard::users.fields.email')), |
|
132
|
|
|
Column::make('created_at')->title(trans('microboard::users.fields.created_at')), |
|
133
|
|
|
Column::computed('action', '') |
|
134
|
|
|
->exportable(false) |
|
135
|
|
|
->printable(false) |
|
136
|
|
|
->width('1%') |
|
137
|
|
|
->addClass('text-right') |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get filename for export. |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function filename() |
|
147
|
|
|
{ |
|
148
|
|
|
return 'User_' . date('YmdHis'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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: