-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCRUDController.php
More file actions
224 lines (183 loc) · 5.32 KB
/
CRUDController.php
File metadata and controls
224 lines (183 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Inertia\Inertia;
/**
* @template T
*/
abstract class CRUDController extends Controller
{
/**
* The model class name.
*
* @var class-string<T>
*/
protected string $model;
/**
* The Inertia view subfolder in the CRUD folder.
*/
protected string $view;
/**
* The validation rules.
*
* @var array<string, string | array<int|string>>
*/
protected array $rules = [];
/**
* The columns to search in.
*
* @var array<int, string>
*/
protected array $search = [];
/**
* The validation rules for the store method.
*
* @param T $old The old model.
* @return array<string, string | array<int|string>>
*/
protected function storeRules(): array
{
return $this->rules;
}
/**
* The validation rules for the update method.
*
* @param T $old The old model.
* @return array<string, string | array<int|string>>
*/
protected function updateRules($old): array
{
return $this->rules;
}
/**
* The array to include with the views.
*
* @return array<mixed, mixed>
*/
protected function with(): array
{
return [];
}
public function index(Request $request)
{
$sort_by = $request->query('sort_by', 'id');
$sort_dir = $request->query('sort_dir', 'asc');
$query = $this->model::orderBy($sort_by, $sort_dir);
$filter_by = $request->query('filter_by');
if ($filter_by) {
foreach ($filter_by as $column => $values) {
$query->whereIn($column, $values);
}
}
$search = $request->query('query');
if ($search) {
$search = explode(' ', $search);
foreach ($search as $searchTerm) {
$query->where(function ($query) use ($searchTerm) {
foreach ($this->search as $column) {
$query->orWhere($column, 'ILIKE', "%{$searchTerm}%");
}
});
}
}
$filteredQuery = collect($request->query())
->intersectByKeys(['sort_by' => '', 'sort_dir' => '', 'query' => '', 'filter_by' => '']);
$items = $query->paginate()->appends($filteredQuery->toArray());
$with = $this->with();
return Inertia::render("CRUD/$this->view/Index", [
'items' => $items,
'with' => $with,
'sortBy' => $sort_by,
'sortDir' => $sort_dir,
'filterBy' => $filter_by,
]);
}
public function show($id)
{
$item = $this->model::find($id);
$with = $this->with();
return Inertia::render("CRUD/$this->view/Show", [
'item' => $item,
'with' => $with,
]);
}
public function create()
{
$with = $this->with();
return Inertia::render("CRUD/$this->view/Create", [
'with' => $with,
]);
}
public function edit($id)
{
$item = $this->model::find($id);
$with = $this->with();
return Inertia::render("CRUD/$this->view/Edit", [
'item' => $item,
'with' => $with,
]);
}
/**
* Gets called after the validation of the store method.
* The returned array will be used to create the new model,
* unless null is returned, in which case no model will be created.
*
* @param array<string, mixed> $new The validated values.
* @return array<string, mixed>|null
*/
protected function created(array $new): ?array
{
return $new;
}
public function store(Request $request)
{
$validated = $request->validate($this->storeRules());
$newValues = $this->created($validated);
if ($newValues !== null) {
$this->model::create($newValues);
}
return redirect()->action([static::class, 'index']);
}
/**
* Gets called after the validation of the update method.
* The returned array will be used to update the model, unless
* null is returned, in which case the model will not be updated.
*
* @param T $old The old model.
* @param array<string, mixed> $new The validated values.
* @return array<string, mixed>|null
*/
protected function updated($old, array $new): ?array
{
return $new;
}
public function update(Request $request, $id)
{
$model = $this->model::find($id);
$validated = $request->validate($this->updateRules($model));
$newValues = $this->updated($model, $validated);
if ($newValues !== null) {
$model->update($newValues);
}
return redirect()->action([static::class, 'index']);
}
/**
* Gets called before the model is deleted.
* If true is returned, the model will be deleted.
* If false is returned, the model will not be deleted.
*
* @param T $old The old model.
*/
protected function destroyed($old): bool
{
return true;
}
public function destroy($id)
{
$model = $this->model::find($id);
if ($this->destroyed($model->toArray())) {
$model->delete();
}
return redirect()->back();
}
}