Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Override Permissions for Super Admin

If you've defined your Gates but want to override all permissions for SUPER ADMIN user, to give that superadmin ALL permissions, you can intercept gates with Gate::before() statement, in AuthServiceProvider.php file.

// Intercept any Gate and check if it's super admin
Gate::before(function($user, $ability) {
if ($user->is_super_admin == 1) {
return true;
}
});
 
// Or if you use some permissions package...
Gate::before(function($user, $ability) {
if ($user->hasPermission('root')) {
return true;
}
});

If you want to do something in your Gate when there is no user at all, you need to add a type hint for $user allowing it to be null. For example, if you have a role called Anonymous for your non-logged-in users:

Gate::before(function (?User $user, $ability) {
if ($user === null) {
$role = Role::findByName('Anonymous');
return $role->hasPermissionTo($ability) ? true : null;
}
return $user->hasRole('Super Admin') ? true : null;
});

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses