3 Common Mistakes I Make When Eager Loading in Laravel

My Valentine Gift To You

What is eager loading? To understand and appreciate what eager loading is, you would need to be conversant with how model relationship works in any MVC framework. I define eager loading in Laravel, from my experience, as a way of loading a particular model's data and getting it's associated data loaded along with it for free. There are two ways this can be done; either by adding the with method on every query request or simply placing it directly on the model as a property so that each time that model is interacted with the expected behavior follows automatically. The content of this post is directed at using it as a property on the model.


Now, when using Laravel's $with attribute on your models it gives you the option to either load everything from the associated model or load only the specific columns that are needed. The former was not an issue but the latter. I most of the time ended up omitting the id of the associated model. What you should know is that for Laravel to load any specific column, it's important that whether you need it or not you include your id column so Laravel can do its magic.

#Load all columns in the associated relationship
protected $with = ['company'];

#Correct way of loading specific columns in the associated relationship
protected $with = ['company:id,name'];

#My mistake
protected $with = ['company:name'];


This one was the very hard one for me to debug initially. Let's look at a sample code first

#Correct way
protected $with = ['company:id,name'];

#This will cause an error
protected $with = ['company:id, name'];

Now, you might be shouting at me and telling me that they are same so why should one work and not the other. You are right to fall for that but what you should know is that every character counts for this to workout. Therefore, the whitespace before the name column will be treated as part of the name and thus when Laravel tries to load your query in the background it will return an error telling you that the column does not exist and it will be so hard for you to notice because the whitespace will most likely not occur to you in your error message.


This one will not produce an error but I felt it's still needed. API resources are simply wrapper classes around your data so that you can map the columns to different names other than the columns in your database tables. It is useful for abstracting the column names in your database table from your frontend.

    'user_id' => $this->id
    'company' => $this->company

The above code will do what $with attribute would have done on your model supposing you run all your database results through the API resource in your controller. What I found myself doing was implementing the aforementioned step and still using the $with on my model. This didn't show anywhere in my code that it was doing a multiple load or something, I think Laravel is intelligent enough to handle it for you, but I believe you must command your code and know what is necessary when.

Eager loading has made life very easy building powerful web apps with Laravel. If you are new to it or would like to read more visit the official docs. If there was something I misunderstood kindly point it out to me. I stand to be corrected . Feel free to follow and reach out to me on my Twitter handle.