我已经使用Laravel一段时间了,而且Laravels渴望加载非常好。但是现在我想在另一个更小的项目中做这件事,我不使用Laravel和“普通”查询。
Currenlty我正在进行两(2)次查询
<button>
和SELECT * FROM questions WHERE id = :id;
所以我希望我的输出:
SELECT * FROM choices WHERE question_id = :id;
对此(我的预期结果):
{
"question": {
"id": "12",
"content": "Question content",
"image_url": "156ebc3206212c_qijpmnklohgfe.jpeg",
"lastmodified": "2016-03-18 09:58:08",
},
"answers": [
{
"id": "45",
"content": "Ja",
"question_id": "12"
},
{
"id": "46",
"content": "nej",
"question_id": "12"
}
]
}
答案栏现在嵌套在{
"question": {
"id": "12",
"content": "Question content",
"image_url": "156ebc3206212c_qijpmnklohgfe.jpeg",
"lastmodified": "2016-03-18 09:58:08",
"answers": [
{
"id": "45",
"content": "Ja",
"question_id": "12"
},
{
"id": "46",
"content": "nej",
"question_id": "12"
}
]
}}
内。 questions
表和answer
表之间的关系是question
列和question_id
列。
问题表
id
答案表
+----+------------------+-----------------------------------+---------------------+
| id | content | image_url | lastmodified |
+----+------------------+-----------------------------------+---------------------+
| 12 | Question content | 156ebc3206212c_qijpmnklohgfe.jpeg | 2016-03-18 09:58:08 |
+----+------------------+-----------------------------------+---------------------+
Laravel称之为急切加载,我不知道它是否是一个连接查询。这是SQLfiddle