Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.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
Php Laravel:在子数组中嵌套查询联接结果_Php_Mysql_Arrays_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel:在子数组中嵌套查询联接结果

Php Laravel:在子数组中嵌套查询联接结果,php,mysql,arrays,laravel,laravel-5,Php,Mysql,Arrays,Laravel,Laravel 5,注意请不要建议使用雄辩,这是专门针对Laravel查询生成器的 出于性能原因,我们使用查询生成器从表中检索结果: DB::table('posts')->get(); 然后,如果我们想将一个关系加入到该查询中: DB:table('posts') ->leftJoin('comments', 'posts.id', '=', 'comments.post_id') ->get(); 结果将合并到每个帖子的数组中: [ 'id'

注意请不要建议使用雄辩,这是专门针对Laravel查询生成器的

出于性能原因,我们使用查询生成器从表中检索结果:

DB::table('posts')->get();
然后,如果我们想将一个关系加入到该查询中:

DB:table('posts')
    ->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
    ->get();
结果将合并到每个帖子的数组中:

[
    'id'                => 1,
    'title'             => 'My Blog Post',
    'content'           => '<h1>This is a post</h1><p>hello world</p>',
    'post_author'       => 'Billy',
    'comment'           => 'This is a comment',
    'comment_author'    => 'Andrew',
]
[
'id'=>1,
“标题”=>“我的博客帖子”,
“内容”=>“这是一篇博文你好,世界

”, “post_author”=>“Billy”, '注释'=>'这是一条注释', “评论作者”=>“安德鲁”, ]
如何将连接的结果放置到嵌套数组中?例如:

[
    'id'                => 1,
    'title'             => 'My Blog Post',
    'content'           => '<h1>This is a post</h1><p>hello world</p>',
    'post_author'       => 'Billy',
    'comment'           => [
        'id'                => 22,
        'comment'           => 'This is a comment',
        'comment_author'    => 'Andrew',            
    ],
]
[
'id'=>1,
“标题”=>“我的博客帖子”,
“内容”=>“这是一篇博文你好,世界

”, “post_author”=>“Billy”, “评论”=>[ “id”=>22, '注释'=>'这是一条注释', “评论作者”=>“安德鲁”, ], ]
如果没有雄辩,不要认为开箱即用是可行的

您可以选择基本路线:

$results = DB:table('posts')
    ->leftJoin('comments', 'posts.id', '=', 'comments.post_id')
    ->select('posts.*', 'comments.*', 'comments.id as comments_id')
    ->get(); 

foreach($results as &$result) 
{ 
    $result['comment'] = [
        'id' => $result['comment_id'], 
        'comment' => $result['comment'], 
        'comment_author' => $result['comment_author']
    ]; 
    unset($result['comment_author'], $result['comment_id']);
}

由于您使用的是DB facade而不是雄辩的,并且无法使用内置的
with()
方法,因此您必须自己实现它:

$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();

foreach($posts as &$post)
{
    $post->comments = array_filter($comments, function($comment) use ($post) {
        return $comment->post_id === $post->id;
    });
}

return $posts;
如果您想删除注释条目的
post\u id
,可以执行以下操作:

$posts = DB::table('posts')->get()->toArray();
$comments = DB::table('comments')->get()->toArray();

foreach($posts as &$post)
{
    $comments = array_filter($comments, function($comment) use ($post) {
        return $comment->post_id === $post->id;
    });

    $post->comments = array_map(function ($comment) {
        unset($comment->id);
        return $comment;
    }, $comments);
}

return $posts;

(我想运行时将类似于
with()
,因为毕竟MySql并没有提供现成的功能)。

尝试定义适当的关系。关系仅用于雄辩的情况。Laravel查询生成器不处理关系@通过查询返回的格式有什么问题?我知道。如果你愿意,那么这很容易做到。@SougataBose问题清楚地表明不要使用Eloquent………我希望Eloquent有一种更微妙的方式来处理结果,至少作为一种选择。你的解决方案并不是将每篇文章的所有评论都分组为一个嵌套数组,实际上每篇文章都会出现N次,N是它的评论数。