65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
|
|
class Student extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'last_name',
|
|
'middle_name',
|
|
'birthday',
|
|
'grade_id',
|
|
];
|
|
|
|
public function grade(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Grade::class);
|
|
}
|
|
|
|
public function user(): MorphOne
|
|
{
|
|
return $this->morphOne(User::class, 'userable');
|
|
}
|
|
|
|
public function subjects(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Subject::class);
|
|
}
|
|
|
|
public function lessons(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Lesson::class);
|
|
}
|
|
|
|
public function scopeFilter(Builder $query): void
|
|
{
|
|
$name = request('name');
|
|
$query->when($name, function (Builder $query, $name) {
|
|
$query->whereRaw('CONCAT (name, \' \', last_name, \' \', middle_name) ilike ?', ["$name%"]);
|
|
$query->orWhereRaw('CONCAT (name, \' \', middle_name, \' \', last_name) ilike ?', ["$name%"]);
|
|
$query->orWhereRaw('CONCAT (last_name, \' \', name, \' \', middle_name) ilike ?', ["$name%"]);
|
|
$query->orWhereRaw('CONCAT (last_name, \' \', middle_name, \' \', name) ilike ?', ["$name%"]);
|
|
$query->orWhereRaw('CONCAT (middle_name, \' \', name, \' \', last_name) ilike ?', ["$name%"]);
|
|
$query->orWhereRaw('CONCAT (middle_name, \' \', last_name, \' \', name) ilike ?', ["$name%"]);
|
|
});
|
|
}
|
|
|
|
public function fio(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name,
|
|
);
|
|
}
|
|
}
|