CourseWork/app/Models/Student.php

65 lines
1.9 KiB
PHP
Raw Normal View History

2024-04-17 11:16:08 +04:00
<?php
namespace App\Models;
2024-05-06 17:51:16 +04:00
use Illuminate\Database\Eloquent\Builder;
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\BelongsTo;
2024-05-07 17:15:58 +04:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2024-04-23 14:52:49 +04:00
use Illuminate\Database\Eloquent\Relations\MorphOne;
2024-05-06 17:51:16 +04:00
2024-04-17 11:16:08 +04:00
class Student extends Model
{
use HasFactory;
protected $fillable = [
'name',
'last_name',
'middle_name',
'birthday',
2024-04-27 15:10:21 +04:00
'grade_id',
2024-04-17 11:16:08 +04:00
];
public function grade(): BelongsTo
{
return $this->belongsTo(Grade::class);
}
2024-04-23 14:52:49 +04:00
public function user(): MorphOne
{
return $this->morphOne(User::class, 'userable');
}
2024-05-07 17:15:58 +04:00
public function subjects(): BelongsToMany
{
2024-05-13 14:43:49 +04:00
return $this->belongsToMany(Subject::class);
}
public function lessons(): BelongsToMany
{
2024-05-17 18:46:12 +04:00
return $this->belongsToMany(Lesson::class);
2024-05-07 17:15:58 +04:00
}
2024-04-23 14:52:49 +04:00
public function scopeFilter(Builder $query): void
{
$name = request('name');
2024-05-06 17:51:16 +04:00
$query->when($name, function (Builder $query, $name) {
2024-06-16 12:20:48 +04:00
$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%"]);
2024-04-23 14:52:49 +04:00
});
}
2024-04-27 15:10:21 +04:00
public function fio(): Attribute
{
return Attribute::make(
2024-05-06 17:51:16 +04:00
get: fn () => $this->last_name.' '.$this->name.' '.$this->middle_name,
2024-04-27 15:10:21 +04:00
);
}
2024-04-17 11:16:08 +04:00
}