How to access the contents of this collection in laravel/php

孤街醉人 提交于 2019-12-02 08:38:46
$friends = Auth::user()->friends();

now the $friends is a collection, which contains a collection of users, please note that $friends does not contain a variable named id rather the each item in collection(The User object) contains an id.

This is where you get the error - ->where('user_id', $friends->id)(here $friends have no id )

so first we take out all the ids of friends, and then take the posts which are made by the friends.

$friends = Auth::user()->friends();
$friends_id = $friends->pluck('id'); //we have all friends id as an array

$posts = Post::where('user_id', $user->id)
               ->whereIn('user_id', $friends_id)
               ->get();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!