Created
November 9, 2021 08:55
-
-
Save Queopius/f3dcc39d09a4bbfca26ba7c8cec31bb5 to your computer and use it in GitHub Desktop.
Get Abort Forbidden for Laravel Permissions with SweetAlert2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Actions\Generic; | |
use Illuminate\Support\Facades\Gate; | |
//use Symfony\Component\HttpFoundation\Response; | |
trait GetAbortForbidden | |
{ | |
protected function abortForbidden(string $permission) | |
{ | |
if ( Gate::denies($permission) ) { | |
return redirect()->back()->with('toast_error', "You don't have permission"); | |
// abort(Response::HTTP_FORBIDDEN, "You don't have permission"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can redirect to a view with a message
Or you can use abort 403 or if you prefer change to 401 (unauthorized)
If you change the HTTP response, it would be better to change the name of the Trait for something more generic. But yourself.👋🏽