class Student extends Model{
public function subjects(){
return $this->belongsToMany(Subject::class, 'student_subjects'); //here student_subjects is as a pivot table
}
}
class Subject extends Model{
public function stdents(){
return $this->belongsToMany(Student::class, 'student_subjects'); //here student_subjects is as a pivot table
}
}
现在保存数据
$student = Student::find(1);
$student->subjects()->attach(2); // it will save subject 2 for student 1 in `student_subjects` table.
1条答案
按热度按时间628mspwn1#
2 db表多对多关系
模型
现在保存数据
注意:类似地,您可以为其他模型创建关系
详情请查收https://laravel.com/docs/5.6/eloquent-relationships#many-对很多人来说