Skip to content

Instantly share code, notes, and snippets.

@MACscr
Last active May 26, 2021 17:15
Show Gist options
  • Save MACscr/095ce5ef6f6f035f7d89a093269b7ffc to your computer and use it in GitHub Desktop.
Save MACscr/095ce5ef6f6f035f7d89a093269b7ffc to your computer and use it in GitHub Desktop.
Simple Role Based Laravel Gates
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