I have a problem with spatie/laravel-permissions
...
I use Gate
in AuthServiceProvider.php
to define Superadmin
(can bypass all permissions without register it to the role)...
It is working perfectly with can('the-permission')
helper.
But it is not working with Auth::user()->hasPermissionTo('the-permission')
...
.
.
Below is my code:
.
In AuthServiceProvider.php
:
public function boot()
{
$this->registerPolicies();
Gate::before(function ($user, $ability) {
$superadmin_rolename = 'Superadmin';
$guard_name = 'web-admin';
return $user->hasRole($superadmin_rolename , $guard_name ) ? true : null;
});
}
.
.
In Blade:
@can('add products')
<button type="submit">Add Product</button>
@endcan
// this will work perfectly, the button will be shown
.
.
In Controller:
public function addProduct()
{
$admin = Auth::guard('web-admin')->user();
if($admin->hasPermissionTo('add products')) return true;
return false;
}
// this is not working (it return false)... i dont know why.... it should return true....
so, as what I show to you above:
- I use
Gate
to define superadmin - the superadmin should grant all access
- it is working perfectly with
can()
and$user->can()
- but it is not working with
$user->hasPermissionTo()
<--------- this is what i want to know
Thanks