Skip to content

Instantly share code, notes, and snippets.

@joshuadegier
Last active June 16, 2016 12:14
Show Gist options
  • Save joshuadegier/e362e7d6dd8c5423d76d4f25c2e05e1e to your computer and use it in GitHub Desktop.
Save joshuadegier/e362e7d6dd8c5423d76d4f25c2e05e1e to your computer and use it in GitHub Desktop.
Blade directive to pass one of the given permissions. The code goes in the boot() method of AppServiceProvider
<?php
/**
* Will pass if the logged in user has access to
* one of the given permissions using Gate::allows.
* Usage:
*
*
* @canone('update-company','company-admin')
* <a href="{{ route('admin.companies.edit', [$company->id]) }}" class="btn btn-smal btn-primary">Modify</a>
* @endif
*/
Blade::directive('canone', function($expression) {
$permissions = explode(",", str_replace(["(", ")", "'", " "], "", $expression));
foreach($permissions as $key => $permission) {
$permissions[$key] = "Gate::allows('{$permission}')";
}
return "<?php if(".implode(" || ", $permissions)."): ?>";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment