Counting Related Models In Laravel
This Laravel quick tip will allow you to count model relations whilst ensuring you don't encounter an N+1 problem.
Very often when retrieving a model in Laravel, it is useful to load a count of related models at the same time. For example, when loading a blog post, you might want to display the number of comments left on that post.
Luckily, Laravel has a method to do just that:
$posts = Post::withCount('comments')->get();
What's more, as of Laravel 8, you can also make use of the
Read more in the Laravel Documentation.
Apple makes some of the most intuitive software around, but if you're coming from another platform, your first Mac experience can be quite confusing. In this article I'll go through some tips and tricks I wish I'd known when I first started using a Mac way back in 2004.