- The application is use spartie
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasRoles;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit articles']);
$users = User::permission('edit articles')->get();
@can('edit articles')
//
@endcan
@if(auth()->user()->can('edit articles') && $some_other_condition)
//
@endif
@role('writer')
I am a writer!
@else
I am not a writer...
@endrole
@hasrole('writer')
I am a writer!
@else
I am not a writer...
@endhasrole
@hasanyrole('writer|admin')
I am either a writer or an admin or both!
@else
I have none of these roles...
@endhasanyrole
For Middelware see https://spatie.be/docs/laravel-permission/v5/basic-usage/middleware
INSERT INTO model_has_roles (role_id, model_type, model_id) VALUES (4, 'App\Models\User', 1);