laravel-permission: BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::givePermissionTo()'
After successfully assigning users to role, I tried calling the givePermissionTo() method to give my roles permission and I get the above error. I added the HasRole trait to my users model as it says in the README. However I’m not quite sure where to put use Spatie\Permission\Models\Role;
and use Spatie\Permission\Models\Permission;
Could this be the cause of the error? Thanks
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (3 by maintainers)
When adding the
HasRole
trait to theUser
model, thegivePermissionTo()
isn’t part of the Eloquent query-builder chain.This works:
but this does not:
In the example above you could change
get()
tofirst()
to make it work:Assign Role worked for me with tinker like this:
I have the same error but with “assingRole” using “tinker”, I have tried everything. My “user” model has “HasRoles”, can create roles without problems, but when I assign it to a user, it gives me the same error. I am about to shoot me in the head.