OneBite.Dev - Coding blog in a bite size

Laravel empty relation when eager loading

Today I faced a problem, where Laravel shows empty results when query a relation with eager loading. Found the solution, it was I forgot to select the id as well, so the relation didn't know what connects the second table with.

Today I faced a problem, where Laravel shows empty results when query a relation with eager loading. Found the solution, it was I forgot to select the id as well, so the relation didn’t know what connects the “second table” with.

So the culprit was, when I query a data by select specific columns

 return Product::with(['tags','reviews'])
                        ->select('title', 'logo', 'snippet', 'slug')

I need to select the id as well

 return Product::with(['tags','reviews'])
                        ->select('id', 'title', 'logo', 'snippet', 'slug')
laravel