在laravel 5.4中,我如何只获得用户好友的帖子? [英] In laravel 5.4 how can i get only the users friends posts?
本文介绍了在laravel 5.4中,我如何只获得用户好友的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$user_id = auth::user()->id;
$user_friends = User::find($user_id)->friends;
$user_friends_id = $user_friends->pluck('id');
$posts = Post::where('user_id', $user_friends_id)->get();
return view('dashboard')->with('posts', $posts);
到目前为止,我已经在$ user_friends_id中获得了用户的好友ID,然后将其与帖子上的用户ID匹配,但是它仅获得了第一个好友的帖子,而没有其他帖子.我如何更改它以获取所有朋友的帖子,而不仅仅是第一个朋友?
So far i've got the users friends id's in $user_friends_id and then matching them to the user id on the posts but its only getting the first friends posts and not the rest. How can i change it to get all the friends posts and not just the first friends?
推荐答案
您无需使用find()
执行其他查询,然后获取所有好友对象只是为了获取其ID.这是巨大的开销.只需这样做:
You don't need to execute an additional query with find()
and then get all friends objects just to get their IDs. It's a huge overhead. Just do this:
Post::whereIn('user_id', auth()->user()->friends()->pluck('id'))->get()
这篇关于在laravel 5.4中,我如何只获得用户好友的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文