crud user

This commit is contained in:
2025-01-13 15:52:38 +03:00
commit aa478c1d8b
68 changed files with 14306 additions and 0 deletions

View File

@ -0,0 +1,8 @@
<?php
namespace App\Http\Controllers;
abstract class Controller
{
//
}

View 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');
}
}

View 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' => []
]);
}
}

34
app/Models/User.php Normal file
View File

@ -0,0 +1,34 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $username
* @property string $email
* @property string $password_hash
* @property string $user_photo
* @property string $access_token
* @property string $access_token_expires_at
* @method static find($id)
*/
class User extends Model {
const DEFAULT_USER_ROLE = 1;
const ADMIN_USER_ROLE = 9;
protected $table = 'user';
protected $fillable = ['username', 'email', 'password_hash', 'user_photo', 'role', 'access_token', 'access_token_expires_at'];
protected array $dates = ['deleted at'];
public static function labels(): array
{
return [
'username' => 'Логин',
'email' => 'Email',
'user_photo' => 'Фото профиля',
'created_at' => 'Создан',
'updated_at' => 'Обновлен',
];
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class UserLaravel extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
//
}
}