Ever since Laravel 5.3, I’ve seen many cases where Eloquent’s relations and their underlying query builders are not used efficiently. Many of these cases exist because there are multiple paths to the same data, many of which look and feel like equivalent solutions. Unless you are familiar with what’s going on under the hood, it’s not always clear which path would be the better one.

The decision to make is usually between $blog->posts and $blog->posts() .

The first will go to the database, fetch all posts related to the blog, and create a Collection of Post models. This is expensive because we have to create and hydrate an instance of every related model.

A good question to ask yourself is “do I need an instance of the model?”. It’s common to only need data for a few attributes. Unless you’re relying on attribute casting or mutators ( Carbon dates for example), you can get these attributes directly from the database.

$blog->posts() will return a Builder straight away, without making any queries yet. The question to ask here is “do I need the entire collection?”. Can the query be more specific? A good policy is to do as much as possible in the database during the query and as little as possible in PHP, for example a where in the database will be much faster than where on a Collection.

Let’s take a look at some examples.