Search code examples
laravellaravel-5eloquenteager-loadinglaravel-5.7

Accessing eager-loaded objects in view laravel 5.7


i have several models that are linked to one model Call i have all the relationships defined and they work pretty okay but i have issues with eager loaded results.

i have this query:

    $callDetails =\App\Call::with(['client','subissues','subissues.issue','partner','district'])
                      ->where('call.id', '=', $id)
                      ->get();

that works fine in tinker returning

{"id":17400,
"client":{"id":18317,"name":"Yad  Kal","phonenumber":"0991234567","age":27,"gender":"Male"},
"district":{"id":1,"name":"Dedza"},
"comments":" He complained ",
"completeness":"complete",
"perpetrator":1,
"partner_id":119,"status":1,
"subissues":[{"id":1378,"name":"Issues of Dissatifaction","description":"Issues of Dissatifaction","issue":{"id":6,"name":"Emergency Response (World)","description":"Emergency response"},"pivot":{"call":17400,"subissue":1378}}],"partner":{"id":119,"name":"World"}} 

but when trying to access client with $callDetails->client, it returns the client id only. and $callDetails->client->name returns an error.

i don't know what am doing wrong but all the eagerloaded results are giving me headaches!


Solution

  • Make sure you don't have in your table column with name client or you don't have any property in your model with name client. When relationship and field/property has exact same name (in your case client) it might cause problems, so in such case you should rename either relationship or field/property