chrislentz's avatar

How would you build a user feed with Laravel?

My site, http://filmfed.com, has a series of different features which members can interact with. For example, a member can add movie ratings and reviews, they can post blog comments, and they can add entries to the message board.

On the users profile, I want to add add a feed that will collect the members interaction from all of the features above and combine them into a central feed that orders everything based on the date of interaction.

I originally thought I would just retrieve the individual model instances, then try to combine the collections. With this plan, I am not sure how I would combine the collections based on the timestamp ordering and and allow for pagination.

Is there a better direction? I am open to suggestions. If you agree with my plan, do you know how I can combine the collections and achieve the pagination?

Thanks!

0 likes
1 reply
mwatson's avatar

You can merge collections

$reviews = collect($user->reviews);
$comments = collect($user->comments);
$otherThings = collect($user->otherThings);

$activities = $reviews->merge($comments)->merge($otherThings)->sortByDesc('created_at')->take(10);

This would retrieve the 10 most recent activities.

Please or to participate in this conversation.