Skip to content

Instantly share code, notes, and snippets.

@Queopius
Created November 9, 2021 08:55
Show Gist options
  • Save Queopius/f3dcc39d09a4bbfca26ba7c8cec31bb5 to your computer and use it in GitHub Desktop.
Save Queopius/f3dcc39d09a4bbfca26ba7c8cec31bb5 to your computer and use it in GitHub Desktop.
Get Abort Forbidden for Laravel Permissions with SweetAlert2
<?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");
}
}
}
@Queopius
Copy link
Author

Queopius commented Nov 9, 2021

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.👋🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment