CourseWork/app/Models/Lesson.php
2024-05-13 14:43:49 +04:00

60 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Contracts\Database\Eloquent\Builder;
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\HasMany;
class Lesson extends Model
{
use HasFactory;
protected $fillable = [
'name',
'type',
'description',
'lesson_date',
'grade_id',
'teacher_id',
'subject_id',
];
public function scores(): HasMany
{
return $this->hasMany(Score::class);
}
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);
}
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);
});
}
}