Support the ongoing development of →
Database Eloquent
Last updated 1 year ago.

You can't do it with eager loading. Moreover it's pretty hard to do in SQL as you would need many subqueries to limit joined rows. So instead load everything and limit comments in php or lazy eager load recent comments.

To do the latter you can use something like this on your Post model to make things easier:

public function commentsRecent()
    return $this->hasMany('Comment')->orderBy('created_at', 'desc')->limit(3);
Last updated 1 year ago.

moh1434 liked this reply


I have found the solution. Try this!

$lines = \Category::where('status', \Category::STATUS_PUBLISH)
			->map(function( $category ){
				$category->firstThreeComment = $category->firstThreeComment->take(3);
				return $category;
Last updated 8 years ago.

Sign in to participate in this thread!


Your banner here too?

tzookb tzookb Joined 9 Feb 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.