71 lines
1.6 KiB
PHP
71 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\TypeLesson;
|
|
use Illuminate\Contracts\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\Support\Carbon;
|
|
|
|
class Lesson extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'type',
|
|
'description',
|
|
'lesson_date',
|
|
'grade_id',
|
|
'teacher_id',
|
|
'subject_id',
|
|
];
|
|
|
|
public function grade(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Grade::class);
|
|
}
|
|
|
|
public function teacher(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Teacher::class);
|
|
}
|
|
|
|
public function subject(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Subject::class);
|
|
}
|
|
|
|
public function students(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Student::class)->withPivot('score');
|
|
}
|
|
|
|
public function scopeFilter(Builder $query): void
|
|
{
|
|
$subject_id = request('subject_id');
|
|
|
|
$query->when($subject_id, function (Builder $query, $subject_id) {
|
|
$query->where('subject_id', $subject_id);
|
|
});
|
|
}
|
|
|
|
public function shortType(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => TypeLesson::getShortType($this->type),
|
|
);
|
|
}
|
|
|
|
protected function date(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => Carbon::parse($this->lesson_date)->format('d-m-Y')
|
|
);
|
|
}
|
|
}
|