crud user
This commit is contained in:
8
app/Http/Controllers/Controller.php
Normal file
8
app/Http/Controllers/Controller.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
114
app/Http/Controllers/UserController.php
Normal file
114
app/Http/Controllers/UserController.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\EditUserRequest;
|
||||
use App\Http\Requests\UserRequest;
|
||||
use App\Models\User;
|
||||
use http\Env\Request;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use itguild\forms\debug\Debug;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function actionIndex($page_number = 1): View|Factory|Application
|
||||
{
|
||||
return view('user.index', [
|
||||
'page_number' => $page_number
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return View|Factory|Application
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionView(int $id): View|Factory|Application
|
||||
{
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
// throw new NotFoundHttpException();
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
return view('user.view', [
|
||||
'user' => User::find($id)
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionCreate(): View|Factory|Application
|
||||
{
|
||||
return view('user.form');
|
||||
}
|
||||
|
||||
public function actionStore(UserRequest $request): View|Factory|Application
|
||||
{
|
||||
$validated = $request->validated();
|
||||
$user = new User();
|
||||
$user->username = $validated['username'];
|
||||
$user->email = $validated['email'];
|
||||
$user->password_hash = Hash::make($validated['password']);
|
||||
$user->save();
|
||||
|
||||
return view('user.view', [
|
||||
'user' => User::find($user->id)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionUpdate(int $id): View|Factory|Application
|
||||
{
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
return view('user.form', [
|
||||
'model' => $user
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function actionEdit(int $id, FormRequest $request): View|Factory|Application
|
||||
{
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
|
||||
$request = UserRequest::rulesForUpdate($request, $user);
|
||||
|
||||
$validated = $request->validated();
|
||||
$user->username = $validated['username'];
|
||||
$user->email = $validated['email'];
|
||||
if (isset($validated['password'])) {
|
||||
$user->password_hash = Hash::make($validated['password']);
|
||||
}
|
||||
$user->save();
|
||||
|
||||
return view('user.view', [
|
||||
'user' => $user
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionDelete(int $id): Application|\Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$user = User::find($id);
|
||||
if (!$user) {
|
||||
throw new \Exception('User not found');
|
||||
}
|
||||
$user->delete();
|
||||
|
||||
return redirect('/admin/user');
|
||||
}
|
||||
}
|
58
app/Http/Requests/UserRequest.php
Normal file
58
app/Http/Requests/UserRequest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class UserRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
// public function authorize(): bool
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|unique:user,username|min:5|max:255',
|
||||
'password' => 'required|min:6|max:255',
|
||||
'email' => 'required|email|unique:user,email|max:255',
|
||||
'user_photo' => ''
|
||||
];
|
||||
}
|
||||
|
||||
public static function rulesForUpdate(FormRequest $formRequest, User $user): \Illuminate\Validation\Validator
|
||||
{
|
||||
return Validator::make($formRequest->all(), [
|
||||
'email' => [
|
||||
'required',
|
||||
'email',
|
||||
'max:255',
|
||||
Rule::unique('user', 'email')->ignore($user->email, 'email'),
|
||||
],
|
||||
'username' => [
|
||||
'required',
|
||||
'min:5',
|
||||
'max:255',
|
||||
Rule::unique('user', 'username')->ignore($user->username, 'username'),
|
||||
],
|
||||
'password' => [
|
||||
'min:6',
|
||||
'max:255',
|
||||
'nullable',
|
||||
],
|
||||
'user_photo' => []
|
||||
]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user