WhosTheKimster
1 month ago
550
10
Laravel

Error accessing (distant) belongsToMany relationship

Posted 1 month ago by WhosTheKimster

I have list of departments that a user can belong to and a list of projects that can belong to many departments. I have set up the belongsToMany relationships for all of these and can access the list of user departments easily as follows:

Auth::user()->departments()

and also the list of projects for each department:

Department::projects()

but when I chain them together:

Auth::user()->departments()->projects()

or combinations thereof e.g.

Auth::user()->departments->projects

I get the error Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsToMany::projects().

Any ideas where I am going wrong?

Please sign in or create an account to participate in this conversation.