1 month ago

Stuck trying to update pivot table with role_id

Posted 1 month ago by ollie_123

Evening All.

I'm trying to update a pivot table with a role_id of a user but it keeps returning an error of Collection::roles does not exist.

I've read the docs multiple times and im struggling to see what i'm missing. My user model has

public function roles()
       return $this->belongsToMany('\App\Role');

and my role model has

public function users()
        return $this->belongsToMany('\App\User');

In the controller i have

$user = \App\User::find($id);
$user->roles()->updateExistingPivot(['user_id' => $id, 'role_id' => $request->role_id]);

but keep getting the error. I'm pretty sure its something obvious that i'm missing but i've been staring at it too long to see.

Please can someone advise where i'm going wrong.

Thank you in advance.

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