Skip to content

Commit a117aa8

Browse files
committed
Restructuring a user controller using Form Request Validation
1 parent f9552da commit a117aa8

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

app/Http/Controllers/Admin/UserController.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Http\Controllers\Admin;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Admin\StoreUserRequest;
7+
use App\Http\Requests\Admin\UpdateUserRequest;
68
use App\Models\User;
79
use App\Models\Role;
810
use Illuminate\Http\Request;
@@ -63,17 +65,11 @@ public function create()
6365
/**
6466
* Store a newly created resource in storage.
6567
*
66-
* @param \Illuminate\Http\Request $request
68+
* @param \App\Http\Requests\Admin\StoreUserRequest $request
6769
* @return \Illuminate\Http\Response
6870
*/
69-
public function store(Request $request)
71+
public function store(StoreUserRequest $request)
7072
{
71-
$request->validate([
72-
'name' => ['required', 'string', 'max:255'],
73-
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
74-
'password' => ['required', 'confirmed', Rules\Password::defaults()],
75-
]);
76-
7773
$user = User::create([
7874
'name' => $request->name,
7975
'email' => $request->email,
@@ -118,18 +114,12 @@ public function edit(User $user)
118114
/**
119115
* Update the specified resource in storage.
120116
*
121-
* @param \Illuminate\Http\Request $request
117+
* @param \App\Http\Requests\Admin\UpdateUserRequest $request
122118
* @param \App\Models\User $user
123119
* @return \Illuminate\Http\Response
124120
*/
125-
public function update(Request $request, User $user)
121+
public function update(UpdateUserRequest $request, User $user)
126122
{
127-
$request->validate([
128-
'name' => ['required', 'string', 'max:255'],
129-
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$user->id],
130-
'password' => ['nullable','confirmed', Rules\Password::defaults()],
131-
]);
132-
133123
$user->update([
134124
'name' => $request->name,
135125
'email' => $request->email,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Admin;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rules;
7+
8+
class StoreUserRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return true;
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array<string, mixed>
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
'name' => ['required', 'string', 'max:255'],
29+
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
30+
'password' => ['required', 'confirmed', Rules\Password::defaults()],
31+
];
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Admin;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rules;
7+
8+
class UpdateUserRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*
13+
* @return bool
14+
*/
15+
public function authorize()
16+
{
17+
return true;
18+
}
19+
20+
/**
21+
* Get the validation rules that apply to the request.
22+
*
23+
* @return array<string, mixed>
24+
*/
25+
public function rules()
26+
{
27+
return [
28+
'name' => ['required', 'string', 'max:255'],
29+
'email' => ['required', 'string', 'email', 'max:255', 'unique:users,email,'.$this->user->id],
30+
'password' => ['nullable','confirmed', Rules\Password::defaults()],
31+
];
32+
}
33+
}

0 commit comments

Comments
 (0)