Reputation: 457
i don't understand why i have this error, i just created the assigneRole and permission, but the assignRole doesn't work, when i use it it says sync or save does not exist, i assume it must be because of the WhereName, any ideas?
This is my User Model:
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function assignRole($role)
{
return $this->roles->save(
Role::whereName($role)->firstOrFail()
);
terminal php artisan tinker
$user = User::latest()->first();
$user->assignRole('editor');
}
Upvotes: 0
Views: 789
Reputation: 6974
Have you try to add () to $this->roles
so make $this->roles()?
$this->roles
return a results
$this->roles()
return a Model relatioship.
There is difference from $this->roles
and $this->roles()
see this post Difference between method calls $model->relation(); and $model->relation;
Upvotes: 2