Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CamKem's avatar
Level 10

Merging data from 2 collections in an activity feed

Hello, I am trying to build an activity feed of authors, or users activity on the blog I am creating as part of a practice exercise.

If I have a collection for 'comments' & a collection for 'posts' that are both related to the user model eg.

$user->posts
$user->comments

How do I go about creating an activity feed that allows me to combine the 2 collections when the data is output in the view so that they are organised in desc date order?

I assume it might be something along the lines of the following but would like your assistance.

$allItems = new \Illuminate\Database\Eloquent\Collection;
$allItems = $allItems->merge($user->posts);
$allItems = $allItems->merge($user->comments);

Then in the view, something like

@foreach($allitems as $allitem)
		//if an item contain the contents of a post use this html formatting
// else and item is a comment & use different html

But I don't fully grasp or understand how I would then have different UI for comments & posts, as I commented, maybe an if statement that looks at the instance of the collection and discerns whether it is a comment or a post?

I am not sure, I appreciate any advice.

0 likes
7 replies
Sinnbeck's avatar

Generally It is a bad idea to merge data of completely different types. What are you trying to do here? Seems a better approach is just two foreach() statements

2 likes
CamKem's avatar
Level 10

@Sinnbeck I am trying to merge both comments & posts displayed in a authors or users profile.

So a profile will have something like "View: All, Posts or Comments" Where when you go to a profile it displays All option automatically and this displays a paginated view of all the comments & posts a user has posted.

This isn't something I have done before, but it is a feature on many website, so I am trying to work out how I would go about doing it & implement it in the project I have going for learning purposes (that you have helped me with)

I don't understand if I use 2 if statements, how will that interlace the html that it output in the view?

Sinnbeck's avatar

@CamKem Two foreach, not if :)

@foreach($posts as $post) 
    //show posts
@endforeach
@foreach($comments as $comment) 
    //show comments
@endforeach

Or is it because you need to display them in random order or something ? So first there is a post, and next there is a comment ?

CamKem's avatar
Level 10

@Sinnbeck So i want to try and have the output from both collections interlaced & initially be displayed in desc order & I will also implement dynamic sorting with other options too.

So in the browser it would look something like this:

User Profile - Viewing All (View Comments, View Posts)

Random Comment Title
Posted 2 Seconds ago

Random Post Title
Posted 10 minutes ago

Random Comment Title 2
Posted 2 days ago

Random Comment Title 3
Posted 3 weeks ago

this is so I can learn how I would go about having the results displayed like this.

CamKem's avatar
Level 10

How about something like?

$posts = $user->posts;
$comments = $user->comments;
$merged = $post->toBase()->merge($comments->toBase());
$sorted = $merged->sortByDesc('created_at');

Then in the view:

@foreach($sorted as $item)
// Display the data
@endforeach

I just don't know how this would handle if there is different values in a comment & a post, combining collections & also then displaying the data in a single @foreach, it might have to have @if / @else statements inside if for how to display each different item (comment or post)

@sinnbeck

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@CamKem If you have the actual classes you can check the try and return a matching type of component

@if ($item instanceof \App\Models\Post)
    <x-post :post="$post" />
@elseif ($item instanceof \App\Models\Comment)
   <x-comment :comment="$comment" />
@endif
webrobert's avatar

I would use an actual activity feed for this. Record Activity. There is actually a (sort of) example for this in the docs. When you need to load model related things to actually display the feed items. See you can load related data for the model type. Then do as @Sinnbeck said.

Also, for the feed poke around here, there are videos on recording events/activity.

Please or to participate in this conversation.