Search code examples
phplaraveleloquent

Laravel Eloquent Lazy Eager Load Count


I'm ideally looking for a function like

load('relationship')

but which loads a count in the same way

withCount('relationship')

works for eager loading.

I'm thinking it is going to be called loadCount('relationship')


Solution

  • loadCount() is available since Laravel 5.8

    $post->loadCount('comments');
    
    $post->comments_count;
    

    Docs