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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user