Last active
May 26, 2021 17:15
-
-
Save MACscr/095ce5ef6f6f035f7d89a093269b7ffc to your computer and use it in GitHub Desktop.
Simple Role Based Laravel Gates
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
Just using a simple `role` field in the User table with a value like 'admin', then setting up the following gate in Providers/AuthServiceProvider.php | |
Gate::define('role', function ($user, ...$roles) { | |
if (in_array($user->role, $roles)) { | |
return true; | |
} | |
}); | |
Then in my livewire component methods and such, I am using the following to check multiple roles: | |
$this->authorize('role', ['admin', 'staff']); | |
Then in my blades, i do something like: | |
@can('role', ['admin', 'staff']) | |
<x-ui::button action="$emit('showModal', 'edit-user', {{ $contact->user_id }})" label="Edit User"/> | |
<x-ui::button action="$emit('showModal', 'create-user', {{ $contact->id }})" label="Create User"/> | |
@endcan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment