1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Microboard\DataTables; |
4
|
|
|
|
5
|
|
|
use Yajra\DataTables\Services\DataTable; |
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 Microboard\Models\Role; |
11
|
|
|
|
12
|
|
|
class RoleDataTable 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
|
|
|
->addColumn('action', function (Role $role) { |
25
|
|
|
$html = ''; |
26
|
|
|
|
27
|
|
|
if (auth()->user()->can('view', $role)) { |
|
|
|
|
28
|
|
|
$html .= '<a href="' . route('microboard.roles.show', $role) . '" ' . |
29
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
30
|
|
|
'data-original-title="' . trans('microboard::roles.view.action-button') . '">' . |
31
|
|
|
'<i class="fas fa-eye"></i></a>'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (auth()->user()->can('update', $role)) { |
35
|
|
|
$html .= '<a href="' . route('microboard.roles.edit', $role) . '" ' . |
36
|
|
|
'class="table-action" data-toggle="tooltip" ' . |
37
|
|
|
'data-original-title="' . trans('microboard::roles.edit.action-button') . '">' . |
38
|
|
|
'<i class="fas fa-edit"></i></a>'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (auth()->user()->can('delete', $role)) { |
42
|
|
|
$html .= '<form action="' . route('microboard.roles.destroy', $role) . '" method="post" class="d-inline-block">' . |
43
|
|
|
csrf_field() . method_field('DELETE') . |
44
|
|
|
'<button type="submit" data-toggle="tooltip" ' . |
45
|
|
|
'class="bg-transparent border-0 p-0 table-action table-action-delete" ' . |
46
|
|
|
'data-original-title="'. trans('microboard::roles.delete.action-button') .'" ' . |
47
|
|
|
'data-modal-title="'. trans('microboard::roles.delete.title') .'" ' . |
48
|
|
|
'data-modal-text="'. trans('microboard::roles.delete.text') .'" ' . |
49
|
|
|
'data-confirm="'. trans('microboard::roles.delete.confirm') .'" ' . |
50
|
|
|
'data-cancel="'. trans('microboard::roles.delete.cancel') .'">' . |
51
|
|
|
'<i class="fas fa-trash"></i></button></form>'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $html; |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get query source of dataTable. |
60
|
|
|
* |
61
|
|
|
* @param Role $role |
62
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
63
|
|
|
*/ |
64
|
|
|
public function query(Role $role) |
65
|
|
|
{ |
66
|
|
|
return $role->newQuery(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Optional method if you want to use html builder. |
71
|
|
|
* |
72
|
|
|
* @return Builder |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function html() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
return $this->builder() |
77
|
|
|
->language(trans('microboard::datatable', [])) |
78
|
|
|
->addTableClass('table table-striped table-hover align-items-center') |
79
|
|
|
->columns($this->getColumns()) |
80
|
|
|
->setTableId('role-table') |
81
|
|
|
->autoWidth(false) |
82
|
|
|
->orderBy(0) |
83
|
|
|
->minifiedAjax() |
84
|
|
|
->dom("<'card'" . |
85
|
|
|
"<'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>>>" . |
86
|
|
|
"<'table-responsive't>" . |
87
|
|
|
"<'card-footer'<'row'<'col-12 col-sm-6'i><'col-12 col-sm-6'p>>>" . |
88
|
|
|
"r>") |
89
|
|
|
->buttons( |
90
|
|
|
Button::make('print')->text('<span>' . trans('microboard::datatable.print') . '</span><i class="fa fa-print"></i>'), |
91
|
|
|
Button::make('excel')->text('<span>' . trans('microboard::datatable.excel') . '</span><i class="fa fa-file-excel"></i>'), |
92
|
|
|
Button::make('reload')->text('<span>' . trans('microboard::datatable.reload') . '</span><i class="fa fa-sync"></i>') |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get columns. |
98
|
|
|
* |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
protected function getColumns() |
102
|
|
|
{ |
103
|
|
|
return [ |
104
|
|
|
Column::make('id')->title(trans('microboard::roles.fields.id'))->width('1%'), |
105
|
|
|
Column::make('name')->title(trans('microboard::roles.fields.name')), |
106
|
|
|
Column::make('display_name')->title(trans('microboard::roles.fields.display_name')), |
107
|
|
|
Column::computed('action', '') |
108
|
|
|
->exportable(false) |
109
|
|
|
->printable(false) |
110
|
|
|
->width('1%') |
111
|
|
|
->addClass('text-right') |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get filename for export. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
protected function filename() |
121
|
|
|
{ |
122
|
|
|
return 'Role_' . date('YmdHis'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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: