You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my latest project, I faced an issue with a collapsible menu where the menu items had permission checks. I wanted the collapsible to show even if only one item had permission to be shown. Here's an example of the menu item:
<x-collapsible>
@can('viewAny', \App\Models\ModelY::class)
<x-item>
Menu Item
</x-item>
@endcan
@can('viewAny', \App\Models\ModelX::class)
<x-item>
Menu Item
</x-item>
@endcan
</x-collapsible>
To solve this, I created a directive called orcan, which allows me to check multiple permissions and return true if at least one of them is granted. Here's the code:
Blade::if('orcan', function (...$permissions): bool {
foreach ($permissions as $permission) {
if (!is_array($permission) || count($permission) !== 2 || !is_string($permission[0]) || !class_exists($permission[1])) {
throw new InvalidArgumentException('Invalid permission argument.');
}
$value = Gate::check($permission[0], $permission[1]);
if ($value) {
return $value;
}
}
return false;
});
Now, I can use the orcan directive in my menu like this:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
In my latest project, I faced an issue with a collapsible menu where the menu items had permission checks. I wanted the collapsible to show even if only one item had permission to be shown. Here's an example of the menu item:
To solve this, I created a directive called orcan, which allows me to check multiple permissions and return true if at least one of them is granted. Here's the code:
Now, I can use the orcan directive in my menu like this:
I think this directive could be a useful addition to the core functionality of Laravel.
Beta Was this translation helpful? Give feedback.
All reactions