Last active
June 16, 2016 12:14
-
-
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
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 | |
/** | |
* 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