Installation Steps
Follow these steps to install and configure Laravel Permission in your application.Install the Package
Install the package via Composer:The package will automatically register its service provider.
Publish the Configuration
Publish the configuration file and migrations:This will create:
config/permission.php- Configuration file- Database migration files in
database/migrations/
You can customize table names, models, and other settings in the config file.
Run the Migrations
Run the migrations to create the necessary database tables:This creates the following tables:
permissions- Stores all permissionsroles- Stores all rolesmodel_has_permissions- Pivot table for direct user permissionsmodel_has_roles- Pivot table for user rolesrole_has_permissions- Pivot table for role permissions
Add the Trait to Your User Model
Add the
HasRoles trait to your User model:app/Models/User.php
The
HasRoles trait also includes the HasPermissions trait, giving you access to all permission methods.Verify Installation
You can verify the installation by creating a permission:Database Schema
Here’s an overview of the tables created by the migrations:permissions
| Column | Type | Description |
|---|---|---|
| id | bigint | Primary key |
| name | varchar | Permission name (e.g., ‘edit articles’) |
| guard_name | varchar | Guard name (e.g., ‘web’, ‘api’) |
| created_at | timestamp | Creation timestamp |
| updated_at | timestamp | Update timestamp |
roles
| Column | Type | Description |
|---|---|---|
| id | bigint | Primary key |
| name | varchar | Role name (e.g., ‘writer’, ‘admin’) |
| guard_name | varchar | Guard name (e.g., ‘web’, ‘api’) |
| created_at | timestamp | Creation timestamp |
| updated_at | timestamp | Update timestamp |
model_has_permissions
| Column | Type | Description |
|---|---|---|
| permission_id | bigint | Foreign key to permissions |
| model_type | varchar | Model class name |
| model_id | bigint | Model ID |
model_has_roles
| Column | Type | Description |
|---|---|---|
| role_id | bigint | Foreign key to roles |
| model_type | varchar | Model class name |
| model_id | bigint | Model ID |
role_has_permissions
| Column | Type | Description |
|---|---|---|
| permission_id | bigint | Foreign key to permissions |
| role_id | bigint | Foreign key to roles |
Configuration
Theconfig/permission.php file contains all configuration options. Here are some key settings:
Models Configuration
Models Configuration
Customize which Eloquent models are used for permissions and roles:You can extend these models with your own custom models.
config/permission.php
Table Names
Table Names
Customize the database table names:
config/permission.php
Cache Settings
Cache Settings
Configure permission caching:Caching significantly improves performance by reducing database queries.
config/permission.php
Teams Feature
Teams Feature
Enable multi-tenancy support:
config/permission.php
Wildcard Permissions
Wildcard Permissions
Enable wildcard permission matching:When enabled, you can use patterns like
config/permission.php
articles.* to match multiple permissions.Seed Sample Data
It’s a good practice to seed your database with initial roles and permissions:database/seeders/PermissionSeeder.php
Troubleshooting
Table 'permissions' doesn't exist
Table 'permissions' doesn't exist
Make sure you’ve run the migrations:If you’ve published the migrations but they’re not running, check that the migration files exist in
database/migrations/.Trait 'HasRoles' not found
Trait 'HasRoles' not found
Clear your autoload cache:
Permissions not working after changes
Permissions not working after changes
Clear the permission cache:Or in code:
Using UUID primary keys
Using UUID primary keys
If your User model uses UUIDs, update the column configuration:
config/permission.php
Next Steps
Basic Concepts
Learn about roles, permissions, and guards
Using Permissions
Start using roles and permissions in your app