grades() ->join('grade_subject', 'grades.id', '=', 'grade_subject.grade_id') ->where('subject_id', $subject->id) ->get(); } public function getGrades(Subject $subject): Collection { return $subject->grades; } public function create(array $data, ?Model $model = null): Teacher { $model->grades()->syncWithoutDetaching($data['grade_id']); return $model; } public function update(Model $model, array $data, ?Model $grade = null): Teacher { $model->grades()->detach($grade->id); $model->grades()->attach($data['grade_id']); return $model; } public function delete(Model $model, ?Model $grade = null): Teacher { $model->grades()->detach($grade); return $model; } }