1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
6
|
|
|
use App\Models\UserRole; |
7
|
|
|
|
8
|
|
|
class UserCrudController extends CrudController |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Prepare the admin interface by setting the associated |
12
|
|
|
* model, setting the route, and adding custom columns/fields. |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
1 |
|
public function setup() : void |
17
|
|
|
{ |
18
|
1 |
|
$this->crud->setModel('App\Models\User'); |
19
|
1 |
|
$this->crud->setRoute('admin/user'); |
20
|
1 |
|
$this->crud->setEntityNameStrings('user', 'users'); |
21
|
|
|
|
22
|
1 |
|
$this->crud->denyAccess('create'); |
23
|
1 |
|
$this->crud->denyAccess('delete'); |
24
|
|
|
|
25
|
1 |
|
$this->crud->addColumn([ |
26
|
1 |
|
'name' => 'name', |
27
|
|
|
'type' => 'text', |
28
|
|
|
'label' => 'Name' |
29
|
|
|
]); |
30
|
1 |
|
$this->crud->addColumn([ |
31
|
1 |
|
'name' => 'email', |
32
|
|
|
'type' => 'text', |
33
|
|
|
'label' => 'Email' |
34
|
|
|
]); |
35
|
1 |
|
$this->crud->addColumn([ |
36
|
1 |
|
'name' => 'user_role.name', |
37
|
|
|
'type' => 'text', |
38
|
|
|
'label' => 'Role' |
39
|
|
|
]); |
40
|
1 |
|
$this->crud->addColumn([ |
41
|
1 |
|
'name' => 'is_priority', |
42
|
|
|
'type' => 'check', |
43
|
|
|
'label' => 'Priority' |
44
|
|
|
]); |
45
|
|
|
|
46
|
1 |
|
$this->crud->addFilter([ |
47
|
1 |
|
'name' => 'user_role', |
48
|
|
|
'type' => 'select2', |
49
|
|
|
'label' => 'Role' |
50
|
|
|
], function () { |
51
|
1 |
|
return UserRole::all()->keyBy('id')->pluck('name', 'id')->toArray(); |
52
|
|
|
}, function ($value) : void { |
53
|
|
|
$this->crud->addClause('where', 'user_role_id', $value); |
54
|
1 |
|
}); |
55
|
|
|
|
56
|
1 |
|
$this->crud->addField([ |
57
|
1 |
|
'name' => 'name', |
58
|
|
|
'label' => 'Name', |
59
|
|
|
'type' => 'text', |
60
|
|
|
'attributes' => [ |
61
|
|
|
'readonly'=>'readonly' |
62
|
|
|
] |
63
|
|
|
]); |
64
|
1 |
|
$this->crud->addField([ |
65
|
1 |
|
'label' => 'Role', |
66
|
|
|
'type' => 'select', |
67
|
|
|
'name' => 'user_role_id', // The db column for the foreign key. |
68
|
|
|
'entity' => 'user_role', // The method that defines the relationship in your Model. |
69
|
|
|
'attribute' => 'name', // Foreign key attribute that is shown to user. |
70
|
|
|
'model' => 'App\Models\UserRole' // Foreign key model. |
71
|
|
|
]); |
72
|
1 |
|
$this->crud->addField([ |
73
|
1 |
|
'name' => 'is_priority', |
74
|
|
|
'type' => 'checkbox', |
75
|
|
|
'label' => 'Priority' |
76
|
|
|
]); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Action for updating an existing User in the database. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Http\Request $request Incoming form request. |
83
|
|
|
* @return \Illuminate\Http\RedirectResponse |
84
|
|
|
*/ |
85
|
|
|
public function update($request) // phpcs:ignore |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$response = parent::updateCrud(); |
88
|
|
|
return $response; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.