Its a many to many relation between questionnaire and question like
class Questionnaire extends Model
{
public function questions()
{
return $this->belongsToMany(Question::class, 'pivot_questionnaire', 'questionnaire _id', 'question_id');
}
}
class Question extends Model
{
public function questionnaires()
{
return $this->belongsToMany(Questionnaire::class, 'pivot_questionnaire', 'question_id','questionnaire_id');
}
}
So if you want to assign multiple questions to questionnaire you can use attach
& sync
$questionIds = [4,5];
$questionnaire->questions()->attach($questionIds);
this will add new associations [4,5] to existing ones like if previously if questionnaire has [1,2,3] then after attach() questionnaire will have [1,2,3,4,5]
$questionnaire->questions()->sync($questionIds);
If you use sync()
it will removed all previous associations and add these new associations
Same goes for questions
$questionnaireIds = [4,5];
$question->questionnaires()->attach($questionnaireIds);
// OR
$question->questionnaires()->sync($questionnaireIds);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…