Skip to content

Commit 72794ad

Browse files
Create UserStoreRequest and finish user store functionality
1 parent 2478301 commit 72794ad

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

app/Http/Controllers/UserController.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Requests\UserStoreRequest;
56
use App\Models\User;
67
use Illuminate\Http\Request;
78
use Inertia\Inertia;
@@ -16,7 +17,7 @@ public function index(Request $request)
1617
return Inertia::render('Users/Index', [
1718
'users' => User::with('role')->when($request->term, function ($query, $term) {
1819
$query->where('name', 'LIKE', '%' . $term . '%');
19-
})->paginate(5),
20+
})->latest()->paginate(5),
2021
]);
2122
}
2223

@@ -31,9 +32,11 @@ public function create()
3132
/**
3233
* Store a newly created resource in storage.
3334
*/
34-
public function store(Request $request)
35+
public function store(UserStoreRequest $request)
3536
{
36-
//
37+
User::create($request->validated());
38+
39+
return to_route('users.index')->with('success', 'User has been created!');
3740
}
3841

3942
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UserStoreRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'name' => 'required|min:2|max:255',
26+
'email' => 'required|email|min:2|max:255|unique:users',
27+
'password' => 'required|min:8|max:255',
28+
'role_id' => 'required',
29+
];
30+
}
31+
}

resources/js/Pages/Users/Create.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export default {
8383
form: this.$inertia.form({
8484
name: '',
8585
email: '',
86-
role_id: 1,
86+
role_id: 2,
8787
password: '',
8888
})
8989
}

0 commit comments

Comments
 (0)