我在查询中使用来获取数据with
,但是在该表中我有另一种关系,并且我想从中获取数据,但不确定如何。
我需要的结果介于question_topics
和之间lk_answers
(这里有topic_1,topic_2 ...的名称)
public function index(Request $request)
{
$query = Question::select(['id', 'free_text', 'title', 'topics_id', 'created_at']);
$query->with('question_topics');
$query->question_topics->with('lkp_answers'); // something like that, but this line is not working.
return response()->json($query->paginate(5));
}
首先所使用的模型question_topics
,你需要有关系best_match_topic
,topic_1
,topic_2
,和topic_3
定义:
例如。QuestionTopic
类
class QuestionTopic {
public function bestMatchTopic() {
return $this->belongsTo(Topic::class, 'best_match_topic');
}
public function topicOne() {
return $this->belongsTo(Topic::class, 'topic_1');
}
public function topicTwo() {
return $this->belongsTo(Topic::class, 'topic_2');
}
public function topicThree() {
return $this->belongsTo(Topic::class, 'topic_3');
}
}
然后,如果要获取关系的关系,则可以使用点符号来访问它们:
Question::with('question_topics.bestMatchTopic',
'question_topics.topicOne',
'question_topics.topicTwo',
'question_topics.topicThree')->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句