问题描述
我有3张这样的桌子
学生: id |名称
class Student extends Model
{
protected $table = 'students';
public function marks () {
return $this->belongsToMany( 'App\Models\Subject','marks' )->using( 'App\Models\Mark');
}
}
主题: id |名称
class Subject extends Model
{
protected $table = 'subjects';
public function marks () {
return $this->belongsToMany( 'App\Models\Student','marks' )->using( 'App\Models\Mark');
}
}
标记: subject_id | student_id |考试类型| times_taking_exam |标记
class Mark extends Pivot
{
protected $table = 'marks';
public function student() {
return $this->belongsTo('App\Models\Student','student_id');
}
public function subject() {
return $this->belongsTo('App\Models\Student','subject_id');
}
public $timestamps = false;
}
具有4列主键(id_subject,id_student,type_of_exam,times_taking_exam)的表Mark
我不知道我的设计数据库是否正确,但是如果确实如此,我该如何使用同步来为学生更新或插入标记。 因为我知道 sync 方法是这样的:
$student->marks()->sync([
$subject_id => [
'type_of_exam' => 1,'times_taking_exam' => 1,'mark' => 10,]
]);
但是这种情况不正确,因为它将运行以下代码:
Mark::updateOrCreate( [
'student_id' => $student_id,'subject_id' => $subject_id,],[
'type_of_exam' => 1,'mark' => 10
] );
相反,我想要这个:
Mark::updateOrCreate( [
'student_id' => $student_id,'subject_id' => $subject_id,'type_of_exam' => 1,[
'mark' => 10
] );
我曾尝试使用“标记为模型”,但我认为这不是最好的方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)