Laravel Query builder with Join table as nested array

余生颓废 提交于 2020-12-15 06:40:10

问题


I have following code. the result i get is a single array. is there any way i can make it a nested array?

$contents = RecentView::where('recent_views.user_id', $loggedUser)
                        ->leftJoin('feed_posts','recent_views.post_id','=','feed_posts.id')
                        ->leftJoin('users','feed_posts.user_id','=','users.id')
                        ->paginate(12)->toArray();

and the result is:

[id] => 71
[user_id] => 71
[post_id] => 529
[site_id] => 103
[date_time] => 2020-11-05 00:00:00
[title] => Lorem Ipsum
[description] => Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem 
                 Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum 
[image] => PiZ0OCrH.jpg
[hit] => 609
[website_id] => 103
[created_at] => 2012-05-02T00:00:00.000000Z
[name] => Franko
[username] => franko
[email] => test@test.com 

But the Result i need is like:

[recent_views]=>
        [id] => 71
        [user_id] => 71
        [post_id] => 529
        [site_id] => 103
        [date_time] => 2020-11-05 00:00:00
[post]=>
        [title] => Lorem Ipsum
        [description] => Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem 
                         Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum 
        [image] => PiZ0OCrH.jpg
        [hit] => 609
        [website_id] => 103
        [created_at] => 2012-05-02T00:00:00.000000Z
[user]=>
        [name] => Franko
        [username] => franko
        [email] => test@test.com 

is there any way to do this in laravel query builder?

来源:https://stackoverflow.com/questions/64880729/laravel-query-builder-with-join-table-as-nested-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!