1//composer require spatie/laravel-permission
2//php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
3//php artisan migrate
4//Add HasRoles trait to the User model
5use Illuminate\Foundation\Auth\User as Authenticatable;
6use Spatie\Permission\Traits\HasRoles;
7
8class User extends Authenticatable
9{
10 use HasRoles;
11
12 // ...
13}
14// Create a role or permission if necessary
15use Spatie\Permission\Models\Role;
16use Spatie\Permission\Models\Permission;
17
18$role = Role::create(['name' => 'writer']);
19$permission = Permission::create(['name' => 'edit articles']);
20//Fetch role
21$users = User::role('writer')->get(); // Returns only users with the role 'writer'