1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\DataTables; |
4
|
|
|
|
5
|
|
|
use App\Models\AdminUser; |
6
|
|
|
use App\Support\Datatables\Services\DataTable; |
7
|
|
|
|
8
|
|
|
class AdminUserDataTable extends DataTable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Build DataTable class. |
12
|
|
|
* |
13
|
|
|
* @return \Yajra\Datatables\Engines\BaseEngine |
14
|
|
|
*/ |
15
|
|
|
public function dataTable() |
16
|
|
|
{ |
17
|
|
|
return $this->datatables |
18
|
|
|
->eloquent($this->query()) |
19
|
|
|
->editColumn('avatar', 'datatables.admin-user-avatar') |
20
|
|
|
->editColumn('action', function ($user) { |
21
|
|
|
return view('datatables.admin-user-action', compact('user'))->render(); |
|
|
|
|
22
|
|
|
}) |
23
|
|
|
->rawColumns(['avatar', 'action']); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the query object to be processed by dataTables. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection |
30
|
|
|
*/ |
31
|
|
|
public function query() |
32
|
|
|
{ |
33
|
|
|
$query = AdminUser::query(); |
34
|
|
|
|
35
|
|
|
return $this->applyScopes($query); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Optional method if you want to use html builder. |
40
|
|
|
* |
41
|
|
|
* @return \Yajra\Datatables\Html\Builder |
42
|
|
|
*/ |
43
|
|
|
public function html() |
44
|
|
|
{ |
45
|
|
|
return $this->builder() |
46
|
|
|
->columns($this->getColumns()) |
47
|
|
|
->parameters($this->getBuilderParameters()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get columns. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function getColumns() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'id' => ['title' => 'ID'], |
59
|
|
|
'avatar' => $this->staticColumn('avatar', ['title' => '头像']), |
60
|
|
|
'username' => ['title' => '用户名'], |
61
|
|
|
'email' => ['title' => '邮箱'], |
62
|
|
|
'created_at' => ['title' => '创建日期'], |
63
|
|
|
'action' => $this->staticColumn('action'), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the default builder parameters. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function getBuilderParameters() |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'order' => [[0, 'asc']], |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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: