Monday, March 20, 2017

How to retrieve only a list of the eager loaded elements in laravel 5?

In my case, I have a user, who has many posts that each have comments and my goal is to retrieve only a collection of comments through the use of eloquent relationships (without creating a custom collection and looping, if possible).

Current I have this:

$comments = $user->posts()->with('comments')->get();

This returns a collection of posts with related comments in each of them, however I wish just a collection of comments.

I hope this makes sense - thanks!



via jacobdo

Advertisement