CourseWork/app/Models/Teacher.php

57 lines
1.8 KiB
PHP
Raw Normal View History

2024-04-17 11:16:08 +04:00
<?php
namespace App\Models;
2024-04-27 15:10:21 +04:00
use Illuminate\Database\Eloquent\Casts\Attribute;
2024-04-17 11:16:08 +04:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2024-04-17 13:02:44 +04:00
use Illuminate\Database\Eloquent\Relations\MorphOne;
2024-04-23 14:52:49 +04:00
use Illuminate\Database\Eloquent\Builder;
2024-04-17 11:16:08 +04:00
class Teacher extends Model
{
use HasFactory;
protected $fillable = [
'name',
'last_name',
'middle_name',
'birthday',
];
public function subjects(): BelongsToMany
{
return $this->belongsToMany(Subject::class);
}
public function grades(): BelongsToMany
{
return $this->belongsToMany(Grade::class);
}
2024-04-17 13:02:44 +04:00
public function user(): MorphOne
{
return $this->morphOne(User::class, 'userable');
}
2024-04-23 14:52:49 +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%"]);
});
}
2024-04-27 15:10:21 +04:00
public function fio(): Attribute
{
return Attribute::make(
get: fn () => $this->last_name . " " . $this->name . " " . $this->middle_name,
);
}
2024-04-17 11:16:08 +04:00
}