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); }); } }