|
3 | 3 | namespace App\Http\Controllers\Admin; |
4 | 4 |
|
5 | 5 | use App\Http\Controllers\Controller; |
| 6 | +use App\Http\Requests\Subject\StoreSubjectRequest; |
| 7 | +use App\Http\Requests\Subject\UpdateSubjectRequest; |
6 | 8 | use App\Models\Node; |
7 | 9 | use App\Models\Subject; |
8 | 10 | use Illuminate\Http\Request; |
@@ -34,55 +36,17 @@ public function edit(Subject $subject) |
34 | 36 | } |
35 | 37 |
|
36 | 38 |
|
37 | | - public function store(Request $request) |
| 39 | + public function store(StoreSubjectRequest $request) |
38 | 40 | { |
39 | | - $validated = $request->validate([ |
40 | | - 'name' => ['required', 'string', 'max:100', 'min:3', 'unique:subjects,name'], |
41 | | - 'tailwind_format' => ['required', 'string', 'max:100'], |
42 | | - 'icon' => ['required', 'string', 'max:50'], |
43 | | - 'sort_order' => ['nullable', 'integer'], |
44 | | - ]); |
45 | | - |
46 | | - $subject = new Subject(); |
47 | | - |
48 | | - $subject->name = $validated['name']; |
49 | | - $subject->tailwind_format = $validated['tailwind_format']; |
50 | | - $subject->slug = Str::slug($validated['name']); |
51 | | - $subject->icon = $validated['icon']; |
52 | | - $subject->sort_order = $validated['sort_order'] ?? 0; |
53 | | - |
54 | | - $subject->save(); |
| 41 | + Subject::create($request->validated()); |
55 | 42 |
|
56 | 43 | return redirect()->route('admin.subjects.index'); |
57 | 44 | } |
58 | 45 |
|
59 | | - public function update(Request $request, Subject $subject) |
| 46 | + public function update(UpdateSubjectRequest $request, Subject $subject) |
60 | 47 | { |
61 | | - $validated = $request->validate([ |
62 | | - 'name' => ['sometimes', 'string', 'max:100', 'min:3', Rule::unique('subjects', 'name')->ignore($subject->id),], |
63 | | - 'tailwind_format' => ['sometimes', 'string', 'max:100'], |
64 | | - 'icon' => ['sometimes', 'string', 'max:50'], |
65 | | - 'sort_order' => ['sometimes', 'nullable', 'integer'], |
66 | | - ]); |
67 | | - |
68 | | - if (isset($validated['name'])) { |
69 | | - $subject->name = $validated['name']; |
70 | | - $subject->slug = Str::slug($validated['name']); |
71 | | - } |
72 | | - |
73 | | - if (isset($validated['tailwind_format'])) { |
74 | | - $subject->tailwind_format = $validated['tailwind_format']; |
75 | | - } |
76 | | - |
77 | | - if (isset($validated['icon'])) { |
78 | | - $subject->icon = $validated['icon']; |
79 | | - } |
80 | | - |
81 | | - if (array_key_exists('sort_order', $validated)) { |
82 | | - $subject->sort_order = $validated['sort_order'] ?? 0; |
83 | | - } |
| 48 | + $subject->update($request->validated()); |
84 | 49 |
|
85 | | - $subject->save(); |
86 | 50 |
|
87 | 51 | return redirect()->route('admin.subjects.index'); |
88 | 52 | } |
|
0 commit comments