In Laravel, how do you use a "cannot" in a Route::middleware group?

711 Views Asked by At

I have my own Laravel gates defined "isAdmin" and "isManager". The "auth" middleware simply means the user is logged in. My routes look like this (greatly simplified):

Route::middleware(['auth', 'can:isAdmin'])->group(
    function () {
        Route::get('/', function() { return response("Admin home"); });
    }
);

Route::middleware(['auth', 'can:isManager'])->group(
    function () {
        Route::get('/', function() { return response("Manager home"); });
    }
);

But what do I do for a route for a logged-in user who is neither an admin nor a manager? I would like to do this:

Route::middleware(['auth', 'cannot:isManager', 'cannot:isAdmin'])->group(
    function () {
        Route::get('/', function() { return response("Who are you, anyway?"); });
    }
);

Anybody have any ideas? Thanks.

1

There are 1 best solutions below

0
On

you can set new rule (gate) as notAdminAndManager in app/Providers/AuthServiceProvider.php on boot method here is some example

Gate::define('isNotAdminAndManager', function (User $user) {
    return (! in_array($);
});