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

saqueib's avatar

Activity feed tutorial way to eager-load relationship by type

I followed @JeffreyWay tutorial for activity feed https://laracasts.com/lessons/build-an-activity-feed-in-laravel

Everything works, but I wanted to eager load some relations, so I did this

Activity::with('subject')->get();

which is giving me all thing mixed with Post, Comment all in one result. I wanted to loaded only Post or Comment. like

Activity::with('post')->get(); 
Activity::with('comment')->get();

How could we do it. should i use where clause to limit the activity by App\Post, or there any other way

0 likes
1 reply
fetch404's avatar
Activity::with('subject')->whereSubjectType('<comment class>')->get();

That should work fine.

Please or to participate in this conversation.