CourseWork/app/Models/Lesson.php

55 lines
1.2 KiB
PHP
Raw Normal View History

2024-05-07 17:16:34 +04:00
<?php
namespace App\Models;
2024-05-13 14:23:21 +04:00
use Illuminate\Contracts\Database\Eloquent\Builder;
2024-05-07 17:16:34 +04:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2024-05-08 12:15:55 +04:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-05-13 14:43:49 +04:00
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2024-05-08 11:39:35 +04:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2024-05-07 17:16:34 +04:00
class Lesson extends Model
{
use HasFactory;
protected $fillable = [
'name',
'type',
2024-05-08 13:34:54 +04:00
'description',
2024-05-07 17:16:34 +04:00
'lesson_date',
2024-05-08 12:59:29 +04:00
'grade_id',
'teacher_id',
'subject_id',
2024-05-07 17:16:34 +04:00
];
2024-05-08 11:39:35 +04:00
2024-05-08 13:34:54 +04:00
public function grade(): BelongsTo
2024-05-08 12:15:55 +04:00
{
return $this->belongsTo(Grade::class);
}
2024-05-08 13:34:54 +04:00
public function teacher(): BelongsTo
2024-05-08 12:15:55 +04:00
{
return $this->belongsTo(Teacher::class);
}
2024-05-08 13:34:54 +04:00
public function subject(): BelongsTo
2024-05-08 12:15:55 +04:00
{
return $this->belongsTo(Subject::class);
}
2024-05-13 14:23:21 +04:00
2024-05-13 14:43:49 +04:00
public function students(): BelongsToMany
{
2024-05-17 18:46:12 +04:00
return $this->belongsToMany(Student::class)->withPivot('score');
2024-05-13 14:43:49 +04:00
}
2024-05-13 14:23:21 +04:00
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);
});
}
2024-05-07 17:16:34 +04:00
}