If you have updated your exising Larafast project, make sure to run migrations to add the necessary tables to your database.
Usage
Creating Roles
There are two ways to create roles in your application:- Using RoleSeeder.php
user
and admin
Run seeder command to add these roles to the database:
- Using Admin Dashboard
/admin/roles
route in your browser and click on the “New Role” button.
Creating Permissions
Permissions can be created in a similar way:- Using PermissionSeeder.php
- Using Admin Dashboard
/admin/permissions
route in your browser and click on the “New Permission” button.
Assigning Roles and Permissions
Roles and permissions can be assigned to users using the Admin Dashboard.-
Visit
/admin/users
route in your browser - Click on the “Edit” button next to the user you want to assign roles/permissions to
- Click on the “Roles” or “Permissions” tab and assign the desired roles/permissions
Middleware
You can protect routes using therole
and permission
middleware provided by the Spatie Laravel Permission package.
Scopes
TheHasRoles
trait also adds role and withoutRole scopes to your models to scope the query to certain roles or permissions:
withoutRole
scopes can accept a string, a \Spatie\Permission\Models\Role
object or an \Illuminate\Support\Collection
object.
The same trait also adds scopes to only get users that have or don’t have a certain permission.
\Spatie\Permission\Models\Permission
object or an \Illuminate\Support\Collection
object.
To learn more about the Spatie Laravel Permission package, refer to the official documentation: Spatie Laravel Permission