In a Laravel 5 app, I have a "User" model and a "Permission" model, both with corresponding tables, that have a many-to-many relationship. There is a pivot table as well: "permission_user".
The User model contains the following method:
public function permissions()
{
return $this->belongsToMany('App\Permission');
}
And the Permission model contains the following method:
public function users()
{
return $this->belongsToMany('App\User');
}
I have been accessing user permissions in custom middleware with the following code, and it's been working splendidly until today.
$permissions = \Auth::user()->permissions()->get();
All of a sudden, this is breaking. I get the following error:
ErrorException in BelongsToMany.php line 177: Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::hydratePivotRelation() must be of the type array, object given, called in /Server/sites/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php on line 158 and defined
Really not sure what's going on here. In an attempt to follow the docs more closely, I also tried this:
foreach (\Auth::user()->permissions as $permission)
{
// do something with $permission
}
But I get the same thing (the stack trace shows that the lines shown here are the last ones executed before heading into the Laravel source). I did update Laravel with Composer around the time this happened, but thought it unlikely that something in Laravel source has caused the problem. Can anyone see what I might be doing wrong here, and how I can fix it?