This is my app\Providers\AuthServiceProvider.php
public function boot(): void
{
Gate::define('hasRole', function ($user, $roles) {
dd($roles);
return in_array($user->role_type, (array) $roles);
});
}
and inside a controller I use
if (!Gate::allows('hasRole', ['Admin', 'Manager'])) {
abort(403, 'Unauthorized');
}
but this dd() only shows first item of array 'Admin' why?
Finally found a solution by myself. Keeping it here for future users who might come up with this issue.
Gate::define('hasRole', function ($user, $roles) {
if (!is_array($roles)) {
$roles = func_get_args();
array_shift($roles); // Remove $user from arguments
}
return in_array($user->role_type, $roles);
});
and this is how I used.
if (!Gate::allows('hasRole', ['Admin'])) {
abort(403, 'Unauthorized');
}