SUBD_Transport_Company/app/Models/User.php

92 lines
2.6 KiB
PHP
Raw Normal View History

2024-01-10 10:57:28 +04:00
<?php
namespace App\Models;
2024-01-16 15:14:13 +04:00
use App\Enums\RoleEnum;
2024-01-30 11:21:30 +04:00
use Illuminate\Database\Eloquent\Builder;
2024-01-16 15:14:13 +04:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-01-10 10:57:28 +04:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2024-01-16 15:14:13 +04:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
2024-01-10 10:57:28 +04:00
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
2024-01-10 11:38:25 +04:00
'surname',
'patronymic',
'phone_number',
2024-01-10 10:57:28 +04:00
'email',
'password',
2024-01-10 11:38:25 +04:00
'role',
2024-01-16 15:14:13 +04:00
'warehouse_id',
2024-01-10 10:57:28 +04:00
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
2024-01-16 15:14:13 +04:00
'role' => RoleEnum::class,
2024-01-10 10:57:28 +04:00
];
public function car(): HasOne
{
return $this->hasOne(Car::class);
}
2024-01-16 15:14:13 +04:00
public function deliveries(): HasMany
{
2024-01-16 15:14:13 +04:00
return $this->hasMany(Delivery::class);
}
2024-01-16 15:14:13 +04:00
public function warehouse(): BelongsTo
{
return $this->belongsTo(Warehouse::class);
}
protected function fio(): Attribute
{
return Attribute::make(
get: fn () => $this->surname . ' ' . $this->name . ' ' . $this->patronymic,
);
}
2024-01-30 11:21:30 +04:00
public function scopeFilter(Builder $query): void
{
$name = request('name');
$query->when($name, function (Builder $query, $name) {
$query->whereRaw('CONCAT (name, \' \', surname, \' \', patronymic) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (name, \' \', patronymic, \' \', surname) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', name, \' \', patronymic) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (surname, \' \', patronymic, \' \', name) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', name, \' \', surname) ilike ?', ["$name%"]);
$query->orWhereRaw('CONCAT (patronymic, \' \', surname, \' \', name) ilike ?', ["$name%"]);
})->paginate(10)->withQueryString();
}
2024-01-10 10:57:28 +04:00
}