Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh

Laravel Eloquent Lazy Eager Load Count


You can use withCount() like this:


$posts = App\Post::withCount('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_count;


You can even eager load relationships count by default by declaring this in your model:


// Post model

protected $withCount = ['comments'];


also you can use loadCount(): loadCount() is available since Laravel 5.8


$post_id = $post->id

$comments_count = $post->comments_count;



Add new comment

Restricted HTML


Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.


Contact Us

All the content is FREE but I still need your help


Become a patreon