Eloquent - Call to undefined relationship Eloquent - Call to undefined relationship laravel laravel

Eloquent - Call to undefined relationship


In my case it was a coding convention issue related to camelCase vs. snake_case:In the Model there was

public function getPermissions()

and in the query there was

User::with(['get_permissions'])

When I changed this to

User::with(['getPermissions'])

it started to work, although I'd say the Laravel way would be to use snake_case instead.

This confused me for a couple of days since frameworks like Symfony and AngularJs has a mixed conventions that somewhere you need to write parameters in snake_case and somewhere else in camelCase. I didn't find any documentation on Laravel site how they handle this, so I tried it the Straight Way and it seemed to be the case here :)


Slapping this here for anyone who may be trying to refactor from an eager load that selects columns to an eager load with a scoped query.

You HAVE to move the selecting of columns into the scoped query, trying to select columns with the usual colon notation will fail and throw the undefined relationship error.

For example going from this eager load that selects a pivot table column, and a column from the permissions table User:with('permissions:permission_tag:tag_set_id,permission_name') to a scoped query that selects the same columns but also orders the results looks like this

  User::with([    'permissions' => function ($query) {      $query->select('permission_tag:tag_set_id', 'permission_name');      $query->orderBy('permission_name');    },  ]);

Notice I pulled out the : notation and it lives right in the scoped query.