我尝试通过关系更新2个模型的值,但是我的方法似乎不太“ Laravel方法”,这样做有更好的方法吗?
$question = new Question();
$question->where('id', $id)->update([
'free_text' => $request->free_text,
'title' => $request->title,
//here I have a topics_id
]);
$question_topics = new QuestionTopics();
$question_topics->where('id', $request->topics_id)->update([
'best_match_topic' => $request->best_match_topic,
'topic_1' => $request->topic_1,
'topic_2' => $request->topic_2,
'topic_3' => $request->topic_3,
]);
从型号:
public function questions()
{
return $this->belongsTo(Question::class);
}
public function question_topics()
{
return $this->hasOne(QuestionTopics::class, 'id');
}
您需要首先在关系中添加param作为topic_id。