Last active
July 27, 2023 04:42
-
-
Save hareom284/3642d09f080d7b5e37b3466e7c860b23 to your computer and use it in GitHub Desktop.
this function help to check policy in laravel
This file contains hidden or 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 | |
if (!function_exists('authorize')) { | |
/* @throws UnauthorizedUserException */ | |
function authorize($ability, $policy, $arguments = []): bool | |
{ | |
if ($policy::{$ability}(...$arguments)) { | |
return false; | |
} | |
return true; | |
} | |
} | |
//In Controler you can called like as below | |
abort_if(authorize('view', StudentPolicy::class), Response::HTTP_FORBIDDEN, '403 Forbidden'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment