Laravel: Nesting query join results in a sub array

后端 未结 1 607
南方客
南方客 2021-01-14 00:15

NOTE Please do not suggest using Eloquent, this is specifically for the Laravel query builder.

For performance reasons we are using Query Builder to

1条回答
  •  别那么骄傲
    2021-01-14 01:04

    Dont think its doable out of the box without Eloquent.

    You can go the primitive route:

    $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']);
    }
    

    0 讨论(0)
提交回复
热议问题