75 lines
2.0 KiB
PHP
75 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\UpdateUserRequest;
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\View\Factory;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
$users = User::all();
|
|
return view('users.index', compact('users'));
|
|
}
|
|
|
|
public function store(UpdateUserRequest $request): RedirectResponse
|
|
{
|
|
if (Auth::guest()) {
|
|
abort(403, 'Вы не авторизованы!');
|
|
}
|
|
|
|
$validated = $request->validated();
|
|
|
|
$user = new User();
|
|
$user->name = $validated['name'];
|
|
$user->password = $validated['password'];
|
|
$user->email = $validated['email'];
|
|
$user->save();
|
|
|
|
return redirect()->route('users.index');
|
|
}
|
|
|
|
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
if (Auth::guest()) {
|
|
abort(403, 'Вы не авторизованы!');
|
|
}
|
|
return view('users.create');
|
|
}
|
|
|
|
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
|
|
{
|
|
if (Auth::guest()) {
|
|
abort(403, 'Вы не авторизованы!');
|
|
}
|
|
return view('users.edit', compact('user'));
|
|
}
|
|
|
|
public function update(UpdateUserRequest $request, User $user): RedirectResponse
|
|
{
|
|
if (Auth::guest()) {
|
|
abort(403, 'Вы не авторизованы!');
|
|
}
|
|
|
|
$validated = $request->validated();
|
|
|
|
$user->fill($validated);
|
|
$user->save();
|
|
|
|
return redirect()->route('users.index');
|
|
}
|
|
|
|
public function destroy(User $user): RedirectResponse
|
|
{
|
|
$user->delete();
|
|
|
|
return redirect()->route('users.index');
|
|
}
|
|
}
|