Skip to content
Advertisement

Eloquent eager loading not working with nested fields

I’m trying to get User model with relations and nested fields. This first try is working fine:

JavaScript

Its returning the entire userdoc object. But I want only two fields, so I’m trying:

JavaScript

And this way it’s returning userdoc:null in toArray() JSON.

This seems to be ok according to with documentation at https://laravel.com/docs/5.5/eloquent-relationships#eager-loading

I’m on Laravel 5.6.

Adding some more information:

Without specific fields gets the entire object

JavaScript

Determining userdoc fields makes the object becomes as null

JavaScript

Advertisement

Answer

You result is because you does not select related primary key user_id. Try this

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