I am using this package https://github.com/renoki-co/befriended My issue is trying to get all my friends posts with my post including user’s data, but the problem is the requester data is null.
$user = $request->user(); $followersPost = UserPost::with(['user' => function ($query) use ($user) { $query->filterFollowingsOf($user); }])->latest()->get(); return $followersPost;
Response
[ { "id": 35, "post_body": "The Requster Post", "location_id": "4b0da1acf964a520b94c23e3", "location_title": "Apple Infinite Loop", "location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png", "location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}", "location_vicinity": "Cupertino - 1 Infinite Loop", "user_id": 1, "viewer_ids": null, "created_at": "2020-12-06T18:08:58.000000Z", "updated_at": "2020-12-06T18:08:58.000000Z", "images": [ ], "musics": [ ], "movies": [ ], "emojs": [ ], "user": null }, { "id": 34, "post_body": "Test Post", "location_id": "4b0da1acf964a520b94c23e3", "location_title": "Apple Infinite Loop", "location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png", "location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}", "location_vicinity": "Cupertino - 1 Infinite Loop", "user_id": 2, "viewer_ids": null, "created_at": "2020-12-06T18:08:14.000000Z", "updated_at": "2020-12-06T18:08:14.000000Z", "images": [ ], "musics": [ ], "movies": [ ], "emojs": [ ], "user": { "id": 2, "_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0", "name": "Saleh", "username": "saleh", "email": "saleh@gmail.com", "email_verified_at": null, "avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg", "location": null, "gender": "male", "bio": null, "birthday": null, "phone": null, "created_at": "2020-12-05T00:27:02.000000Z", "updated_at": "2020-12-05T00:27:21.000000Z" } }, { "id": 24, "post_body": null, "location_id": null, "location_title": null, "location_icon": null, "location_coordinate": null, "location_vicinity": null, "user_id": 3, "viewer_ids": null, "created_at": "2020-12-06T01:53:18.000000Z", "updated_at": "2020-12-06T01:53:18.000000Z", "images": [ ], "musics": [ { "id": 4, "post_id": 24, "song_name": "Confetti", "song_preview": "https://cdns-preview-6.dzcdn.net/stream/c-6eb643a05083c023f874f13739fc7c87-3.mp3", "song_picture": "https://api.deezer.com/album/183603832/image", "song_artist": "Little Mix", "created_at": "2020-12-06T01:53:18.000000Z", "updated_at": "2020-12-06T01:53:18.000000Z" } ], "movies": [ ], "emojs": [ { "id": 16, "post_id": 24, "user_id": 3, "emojy": "015-smile-1", "created_at": "2020-12-06T01:59:15.000000Z", "updated_at": "2020-12-06T01:59:15.000000Z", "user": { "id": 3, "_id": "af65864b-f18a-4609-91db-336acbce1486", "name": "Ali", "username": "ali2323", "email": "ali@gmail.com", "email_verified_at": null, "avatar": null, "location": null, "gender": "male", "bio": null, "birthday": null, "phone": null, "created_at": "2020-12-05T04:05:50.000000Z", "updated_at": "2020-12-05T04:05:50.000000Z" } }, { "id": 17, "post_id": 24, "user_id": 1, "emojy": "089-broken-heart", "created_at": "2020-12-06T16:58:07.000000Z", "updated_at": "2020-12-06T16:58:24.000000Z", "user": { "id": 1, "_id": "737ba284-424d-4b0d-8a56-2e845603f2ba", "name": "Mohammed", "username": "mohammed", "email": "h4ck3r.x0@gmail.com", "email_verified_at": null, "avatar": "http://backend.mf.sa/storage/avatars/1607128001.jpg", "location": null, "gender": "male", "bio": null, "birthday": null, "phone": null, "created_at": "2020-12-05T00:26:09.000000Z", "updated_at": "2020-12-05T00:26:41.000000Z" } }, { "id": 19, "post_id": 24, "user_id": 2, "emojy": "021-sad", "created_at": "2020-12-06T16:58:36.000000Z", "updated_at": "2020-12-06T16:58:36.000000Z", "user": { "id": 2, "_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0", "name": "Saleh", "username": "saleh", "email": "saleh@gmail.com", "email_verified_at": null, "avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg", "location": null, "gender": "male", "bio": null, "birthday": null, "phone": null, "created_at": "2020-12-05T00:27:02.000000Z", "updated_at": "2020-12-05T00:27:21.000000Z" } } ], "user": { "id": 3, "_id": "af65864b-f18a-4609-91db-336acbce1486", "name": "Ali", "username": "ali2323", "email": "ali@gmail.com", "email_verified_at": null, "avatar": null, "location": null, "gender": "male", "bio": null, "birthday": null, "phone": null, "created_at": "2020-12-05T04:05:50.000000Z", "updated_at": "2020-12-05T04:05:50.000000Z" } } ]
As you can see the first object in the array dosen’t include the user data.
So what should i do $query->filterFollowingsOf($user);
to include my data?
Advertisement
Answer
I think the with
callback is used for modifying the query that selects the user data. Instead, try using a whereHas
method that filters the related users.
$user = $request->user(); $followersPost = UserPost ::with('user') ->whereHas('user', function ($query) use ($user) { $query->filterFollowingsOf($user); }) ->latest() ->get(); return $followersPost;
I’d also recommend using paginate()
instead of get()
for performance when there could be a significant number of posts.
Edit:
To get either your friend’s posts or your posts, try:
$followersPost = UserPost ::with('user') ->whereHas('user', function ($query) use ($user) { $query->filterFollowingsOf($user); }) ->orWhere('user_id', $user->id) ->latest() ->get();