Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel DB查询:获取一对多多维数组结果_Laravel_Laravel 5.3 - Fatal编程技术网

Laravel DB查询:获取一对多多维数组结果

Laravel DB查询:获取一对多多维数组结果,laravel,laravel-5.3,Laravel,Laravel 5.3,我是拉威尔的新手, 让我直接回答这个问题 假设我有“Posts”表和“Comments”表 第一个“帖子”有3条评论 实现这种结果的最佳查询方法是什么 Post => ( [0]=> array ( [id]=>1 [content]=> This is my first Post! [comments]=> array( [0]=>array( [id]=>1

我是拉威尔的新手, 让我直接回答这个问题

假设我有“Posts”表和“Comments”表

第一个“帖子”有3条评论

实现这种结果的最佳查询方法是什么

Post => (
   [0]=> array (
      [id]=>1
      [content]=> This is my first Post!
      [comments]=> array(
         [0]=>array(
            [id]=>1
            [post_id]=>1
            [content]=> First comment!
         )

         [1]=>array(
            [id]=>2
            [post_id]=>1
            [content]=> Second comment!
         )

         [2]=>array(
            [id]=>3
            [post_id]=>1
            [content]=> Third comment!
         )


      )
   )
)

你可以使用雄辩的人际关系


这个例子正是你想要的!(:

急切地加载关系:

$posts = Post::with('comments')->get()->toArray();

不,它只返回注释值mazenin:请帮助:(@Angger您需要先定义
hasMany()
关系才能使其工作。