Skip to content

Instantly share code, notes, and snippets.

@hareom284
Last active July 27, 2023 04:42
Show Gist options
  • Save hareom284/3642d09f080d7b5e37b3466e7c860b23 to your computer and use it in GitHub Desktop.
Save hareom284/3642d09f080d7b5e37b3466e7c860b23 to your computer and use it in GitHub Desktop.
this function help to check policy in laravel
<?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