2 回答

TA貢獻1784條經驗 獲得超9個贊
我建議您需要參考https://laravel.com/docs/5.8/eloquent-relationships#has-many-through鏈接來實現這一點,
在學生模式中添加hasManyThrough關系。
public function courses()
{
return $this->hasManyThrough(
'App\Course',
'App\Grade',
'course_id', // Foreign key on Grade table...
'id', // Foreign key on Course table...
'id', // Local key on Student table...
'student_id' // Local key on Grade table...
);
}
我沒有測試過這段代碼,但是,這就是方式

TA貢獻1842條經驗 獲得超22個贊
讓成績表成為學生和課程之間多對多關系的數據透視表怎么樣?
class Student
{
// an alternative name could be gradedCourses.
public function courses()
{
return $this->belongsToMany(Course::class, 'grades');
}
}
因此,您可以直接從這種關系中訪問課程。
@foreach($student->courses course)
$course->name
@endforeach
- 2 回答
- 0 關注
- 158 瀏覽
添加回答
舉報