1
|
|
|
<?php namespace App\Http\Controllers\Admin; |
2
|
|
|
|
3
|
|
|
use App\Http\Requests; |
4
|
|
|
use App\Http\Controllers\Controller; |
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
9
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
10
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
11
|
|
|
|
12
|
|
|
class ExampleCrudController extends CrudController { |
13
|
|
|
|
14
|
|
|
public function __construct() { |
15
|
|
|
parent::__construct(); |
16
|
|
|
|
17
|
|
|
/* |
18
|
|
|
|-------------------------------------------------------------------------- |
19
|
|
|
| API |
20
|
|
|
|-------------------------------------------------------------------------- |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
// USAGE LEVEL 1 - ALWAYS ================================================== LEVEL 1 |
25
|
|
|
$this->crud->setModel("App\Models\Example"); |
26
|
|
|
$this->crud->setRoute("admin/example"); |
27
|
|
|
// $this->crud->setRouteName("admin.example"); |
|
|
|
|
28
|
|
|
$this->crud->setEntityNameStrings("example", "examples"); |
29
|
|
|
|
30
|
|
|
// $this->crud->setColumns(); // set the columns you want in the table view, either as array of column names, or multidimensional array with all columns detailed with their types |
|
|
|
|
31
|
|
|
// TODO: $this->crud->setFields($array_of_arrays, 'update/create/both'); // set fields for both create&update forms |
|
|
|
|
32
|
|
|
// $this->crud->setFromDb(); // automatically set fields and columns from your database columns TODO: rephrase |
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
// USAGE LEVEL 2 - OFTEN ================================================== LEVEL 2 |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
// ------ FIELDS (the last parameter is always the form - create/update/both) |
40
|
|
|
// TODO: $this->crud->addField('name', $options, 'update/create/both'); |
|
|
|
|
41
|
|
|
// TODO: $this->crud->addFields($array_of_arrays, 'update/create/both'); |
|
|
|
|
42
|
|
|
// TODO: $this->crud->removeField('name', 'update/create/both'); |
|
|
|
|
43
|
|
|
// TODO: $this->crud->removeFields($array_of_names, 'update/create/both'); |
|
|
|
|
44
|
|
|
// TODO: $this->crud->replaceField('name', 'update/create/both'); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// TODO: $this->crud->setRequiredFields(['field_1', 'field_2'], 'update/create/both'); |
|
|
|
|
47
|
|
|
// TODO: $this->crud->setRequiredField('field_1', 'update/create/both'); |
|
|
|
|
48
|
|
|
// TODO: $this->crud->getRequiredFields(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
// TODO: $this->crud->setFieldOrder(['field_1', 'field_2', 'field_3'], 'update/create/both'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
// ------ COLUMNS |
54
|
|
|
// $this->crud->addColumn(); // add a single column, at the end of the stack |
|
|
|
|
55
|
|
|
// $this->crud->addColumns(); // add multiple columns, at the end of the stack |
|
|
|
|
56
|
|
|
// $this->crud->removeColumn('column_name'); // remove a column from the stack |
|
|
|
|
57
|
|
|
// $this->crud->removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack |
|
|
|
|
58
|
|
|
// $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); |
|
|
|
|
59
|
|
|
// $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']); |
|
|
|
|
60
|
|
|
// TODO: $this->crud->setColumnOrder(['column_1', 'column_2', 'column_3']); |
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
// ------ FIELDS AND COLUMNS |
64
|
|
|
// TODO: $this->crud->setLabel('column_name/field_name', 'New Label'); // changes label for columns, create&update fields |
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
// ------ ACCESS |
68
|
|
|
// $this->crud->allowAccess('list'); |
|
|
|
|
69
|
|
|
// $this->crud->allowAccess(['list', 'create', 'delete']); |
|
|
|
|
70
|
|
|
// $this->crud->denyAccess('list'); |
|
|
|
|
71
|
|
|
// $this->crud->denyAccess(['list', 'create', 'delete']); |
|
|
|
|
72
|
|
|
|
73
|
|
|
// $this->crud->hasAccess('add'); // returns true/false |
|
|
|
|
74
|
|
|
// $this->crud->hasAccessOrFail('add'); // throws 403 error |
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
// ------ REORDER |
78
|
|
|
// $this->crud->enableReorder('label_name', MAX_TREE_LEVEL); |
|
|
|
|
79
|
|
|
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder'); |
80
|
|
|
|
81
|
|
|
// $this->crud->disableReorder(); |
|
|
|
|
82
|
|
|
// $this->crud->isReorderEnabled(); // return true/false |
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
// ------ DETAILS ROW |
86
|
|
|
// $this->crud->enableDetailsRow(); |
|
|
|
|
87
|
|
|
// NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row'); |
88
|
|
|
// NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php |
89
|
|
|
|
90
|
|
|
// $this->crud->disableDetailsRow(); |
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
// ------ ADVANCED QUERIES |
94
|
|
|
// $this->crud->addClause('active'); |
|
|
|
|
95
|
|
|
// $this->crud->addClause('type', 'car'); |
|
|
|
|
96
|
|
|
// $this->crud->addClause('where', 'name', '==', 'car'); |
|
|
|
|
97
|
|
|
// $this->crud->addClause('whereName', 'car'); |
|
|
|
|
98
|
|
|
// $this->crud->addClause('whereHas', 'posts', function($query) { |
|
|
|
|
99
|
|
|
// $query->activePosts(); |
|
|
|
|
100
|
|
|
// }); |
101
|
|
|
// $this->crud->orderBy(); |
|
|
|
|
102
|
|
|
// $this->crud->groupBy(); |
|
|
|
|
103
|
|
|
// $this->crud->limit(); |
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
// USAGE LEVEL 3 - SOMETIMES ============================================== LEVEL 3 |
108
|
|
|
|
109
|
|
|
// TODO: $this->crud->setButtons(); // default includes edit and delete, with their name, icon, permission, link and class (btn-default) |
|
|
|
|
110
|
|
|
// TODO: $this->crud->addButton(); |
|
|
|
|
111
|
|
|
// TODO: $this->crud->removeButton(); |
|
|
|
|
112
|
|
|
// TODO: $this->crud->replaceButton(); |
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
// USAGE LEVEL 4 - RARELY ================================================== LEVEL 4 |
117
|
|
|
|
118
|
|
|
// $this->crud->getEntry($entry_id); |
|
|
|
|
119
|
|
|
// $this->crud->getEntries(); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// $this->crud->getFields('create/update/both'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// $this->crud->create($entry_request); |
|
|
|
|
124
|
|
|
// $this->crud->update($entry_id, $entry_request); |
|
|
|
|
125
|
|
|
// $this->crud->delete($entry_id); |
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
// USAGE LEVEL 5 - ALMOST NEVER ============================================== LEVEL 5 |
129
|
|
|
|
130
|
|
|
// $this->crud->updateTreeOrder($all_entries); |
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
// ------------------------ |
136
|
|
|
// MEANWHILE THIS WILL WORK |
137
|
|
|
// ------------------------ |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->crud->reorder = true; |
141
|
|
|
$this->crud->reorder_label = "name"; |
|
|
|
|
142
|
|
|
$this->crud->reorder_max_level = 3; |
143
|
|
|
$this->crud->details_row = true; |
144
|
|
|
// $this->crud->permissions = ['add', 'list', 'edit', 'delete', 'show']; |
|
|
|
|
145
|
|
|
|
146
|
|
|
$this->crud->columns = [ |
147
|
|
|
[ |
148
|
|
|
'name' => 'name', |
149
|
|
|
'label' => "Example item text" |
150
|
|
|
], |
151
|
|
|
[ |
152
|
|
|
'label' => "Parent", |
153
|
|
|
'type' => 'select', |
154
|
|
|
'name' => 'parent_id', |
155
|
|
|
'entity' => 'parent', |
156
|
|
|
'attribute' => 'name', |
157
|
|
|
'model' => "App\Models\Example" |
158
|
|
|
], |
159
|
|
|
]; |
160
|
|
|
$this->crud->fields = [ |
161
|
|
|
[ |
162
|
|
|
'name' => 'name', |
163
|
|
|
'label' => "Example item text" |
164
|
|
|
], |
165
|
|
|
[ |
166
|
|
|
'label' => "Parent", |
167
|
|
|
'type' => 'select', |
168
|
|
|
'name' => 'parent_id', |
169
|
|
|
'entity' => 'parent', |
170
|
|
|
'attribute' => 'name', |
171
|
|
|
'model' => "App\Models\Example" |
172
|
|
|
], |
173
|
|
|
[ |
174
|
|
|
'name' => 'type', |
175
|
|
|
'label' => "Type", |
176
|
|
|
'type' => 'page_or_link' |
177
|
|
|
], |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function store(StoreRequest $request) |
183
|
|
|
{ |
184
|
|
|
return parent::storeCrud(); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function update(UpdateRequest $request) |
188
|
|
|
{ |
189
|
|
|
return parent::updateCrud(); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.