I thinks its already clear in the documentation, but here are the headlines
// Paper.php model
public function questions()
{
return $this->belongsToMany('App\Question')->withPivot('question_score');
}
// Question.php model
public function papers()
{
return $this->belongsToMany('App\Paper')->withPivot('question_score');
}
// anywhere in controllers
// update paper's questions
$paper->questions()->sync([
$question_1_id => ['question_score' => 'your_value'],
$question_2_id => ['question_score' => 'your_value'],
]); // assuming that: $paper is valid instance, $question_1_id & $question_2_id are valid ids
// update question's papers
$question->papers()->sync([
$paper_1_id => ['question_score' => 'your_value'],
$paper_2_id => ['question_score' => 'your_value'],
]);
Hope it helps.
farshidgh70 liked this reply
just assume I have an array where array keys are question_id and array values are question_scores how can I parse it in sync method for updating the pivot table.
// question with id 1 has a question_score 100.
$data = array(1 => 100);
$syncData = array();
foreach($data as $id => $score){
$syncData[$id] = array('question_score' => $score);
}
$paper->questions()->sync($syncData);
farshidgh70 liked this reply
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community