Skip to content
Advertisement

laravel relationship not loading

I’ve got a Product and a Category models:

JavaScript
JavaScript

In my controller, I’m retrieveing all products and wanted to return the category object the product belongs to in the response, so I’m doing:

JavaScript

and my resource looks like:

JavaScript

but the response I’m getting is:

JavaScript

I tried loading the relationship differently:

JavaScript

but the result is the same.

It seems that the relationship is well established because if I run:

JavaScript

I can see the category the product belongs to:

JavaScript

what am I missing?

Advertisement

Answer

In controller load category relation with eager loading

JavaScript

and in the ProductsResource file load, the relation category, not category_id

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement