applicant-site/app/Http/Controllers/UserController.php

75 lines
2.0 KiB
PHP
Raw Normal View History

2024-01-11 15:10:01 +03:00
<?php
namespace App\Http\Controllers;
2024-01-12 16:42:16 +03:00
use App\Http\Requests\UpdateUserRequest;
2024-01-11 15:10:01 +03:00
use App\Models\User;
2024-01-12 16:42:16 +03:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Foundation\Application;
use Illuminate\Http\RedirectResponse;
2024-01-11 15:10:01 +03:00
use Illuminate\Support\Facades\Auth;
class UserController extends Controller
{
2024-01-12 16:42:16 +03:00
public function index(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
2024-01-11 15:10:01 +03:00
{
$users = User::all();
return view('users.index', compact('users'));
}
2024-01-12 16:42:16 +03:00
public function store(UpdateUserRequest $request): RedirectResponse
2024-01-11 15:10:01 +03:00
{
if (Auth::guest()) {
2024-01-12 09:56:06 +03:00
abort(403, 'Вы не авторизованы!');
2024-01-11 15:10:01 +03:00
}
$validated = $request->validated();
2024-01-12 16:42:16 +03:00
$user = new User();
$user->name = $validated['name'];
$user->password = $validated['password'];
$user->email = $validated['email'];
$user->save();
2024-01-11 15:10:01 +03:00
2024-01-12 16:42:16 +03:00
return redirect()->route('users.index');
}
public function create(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
2024-01-11 15:10:01 +03:00
}
2024-01-12 16:42:16 +03:00
return view('users.create');
}
2024-01-11 15:10:01 +03:00
2024-01-12 16:42:16 +03:00
public function edit(User $user): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
return view('users.edit', compact('user'));
2024-01-11 15:10:01 +03:00
}
2024-01-12 09:56:06 +03:00
2024-01-12 16:42:16 +03:00
public function update(UpdateUserRequest $request, User $user): RedirectResponse
2024-01-12 09:56:06 +03:00
{
if (Auth::guest()) {
abort(403, 'Вы не авторизованы!');
}
2024-01-12 16:42:16 +03:00
$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');
2024-01-12 09:56:06 +03:00
}
2024-01-11 15:10:01 +03:00
}