CourseWork/app/Models/Lesson.php
m.zargarov 92efb4498d Fixed
2024-05-27 16:57:28 +04:00

55 lines
1.2 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 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);
});
}
}