I'm creating a newsfeed system where in users can post and share posts.
What I got so far is:
Database Structuretbl_post
:id, user_id, ...
tbl_shared_post
:id, post_id, shared_by
Post Retrieval Query : Eloquent
public function feedgetpost(Request $request) { //Pluck the IDs of the current user's followers $followers = UserFollower::where('status', 'accepted') ->where('target_user_id', $currentUserID) ->pluck('follower_id')->toArray(); //Pluck the IDs of the current user's followings $followings = UserFollower::where('status', 'accepted') ->where('follower_id', $currentUserID) ->pluck('target_user_id')->toArray(); $feed= Feed::where('is_delete', 0) ->whereIn('user_id', array_merge($followers, $following, [$request->user_id])) //Get following/followers post as well ->limit(10) ->orderBy('id', 'DESC') ->get(); $arrays = []; foreach ($feed as $post) { $post['shares'] = PostShare::getSharesByPostID($post->id); $arrays[] = $post; } } return $arrays;}
But the code above returns data in this way:
{ caption: "This is a test upload only. Have fun :)!" created_at: "2021-02-16T08:11:55.000000Z" id: 1 is_delete: 0 shares: [ {'id':0, 'post_id': 1, 'shared_by': 3}, {'id':1, 'post_id': 1, 'shared_by': 5}, {'id':2, 'post_id': 1, 'shared_by': 14} ]}
Now, based on the result set. I can't really render the shared post but only get the count of it.
My question is that, How do I make it so that:
Scenario 1.OP
post something.
Scenario 2.User B
shared OP
's post, and then a share count
of 1 gets credited to OP
's post.
Scenario 3.User C
shared User B
's shared post, and a share count
of 1 gets credited to User B
's post.
and so on...