Download the PHP package erag/laravel-role-permission without Composer
On this page you can find all versions of the php package erag/laravel-role-permission. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download erag/laravel-role-permission
More information about erag/laravel-role-permission
Files in erag/laravel-role-permission
Package laravel-role-permission
Short Description A simple and easy-to-install role and permission management package for Laravel, supporting versions 10.x and 11.x
License MIT
Homepage https://github.com/eramitgupta/laravel-role-permission
Informations about the package laravel-role-permission
Laravel Role-Permission
Getting Started
Install the package via Composer
Step 1: Add Trait to User Model
Before configuring the database and publishing the role-permission files, add the HasPermissionsTrait
to define in your User
model. This trait is essential for handling roles and permissions in your application.
Step 2: Database Configuration
Before proceeding with the setup, ensure that your database connection is properly configured in your .env
file. Example configuration:
Make sure to replace your_database_name
, your_database_user
, and your_database_password
with your actual database credentials.
Step 3: Register the Service Provider
For Laravel v11.x
Ensure the service provider is registered in your /bootstrap/providers.php
file:
For Laravel v8.x, v9.x, v10.x
Ensure the service provider is registered in your config/app.php
file:
Step 4: Publish Role-Permission Files
Once the database is configured, publish the required migration with a single command:
You can also run migrations and seeders:
Or both
Upgrade New Version Command
To upgrade the package to a new version:
Available Functions Documentation
Permission Management
givePermissionsTo(array $permissions, string|array|null $expiresAt = null): static
Assigns permissions to a user with optional expiration dates.
$permissions
: Array of permission names or objects$expiresAt
: Can be null (no expiration), a string date, or array of dates mapped to permission names
detachPermissions(string|array $permissions): static
Removes specified permissions from a user.
hasPermissionTo(...$permissions): bool
Checks if user has specific permissions through roles or direct assignment.
hasPermissions(string|array $permissions): bool
Verifies user has all specified permissions.
Role Management
assignRole(string|array $roles): static
Assigns roles to a user.
hasRole(...$roles): bool
Checks if user has any of the specified roles.
Helper Methods
hasPermissionThroughRole($permission): bool
Checks if user has permission through assigned roles.
roles()
Relationship method for user roles.
permissions()
Relationship method for user permissions.
Using Role-Based Permissions
You can now easily check user permissions within your application logic: You can also use the helper method:
OR
Retrieve Permissions and Roles
Using Role-Based Checks
Blade Directives
Middleware Usage
How to Use Permissions Expiration
The permission expiration feature allows you to set temporary access that expires automatically after a certain period or, by setting the expiration date to null, to allow unlimited access. This feature is useful for setting up both temporary and permanent permissions.
Adding Permissions with Expiration
- Assign Permission with Expiration: Use the
givePermissionsTo
method to assign a permission with an expiration date.
In this example, the post-create
permission will be assigned to the user and expire after 30 days.
- Assign Multiple Permissions with Different Expirations: If you need to assign multiple permissions with individual expiration dates, pass an associative array where the keys are permission names, and the values are the expiration dates.
How to Use without Permissions Expiration
- Assign Permission with Unlimited Duration: Assign permissions without an expiration by setting the expiration to
null
. This will give the user unlimited access to the permission.
OR
Example Seeder for Roles and Permissions
Here's an example RolePermissionSeeder
that seeds roles, permissions, and users:
Contribution 🧑💻
We welcome contributions to this project. Please read our Contributing Guidelines before you start contributing.
All versions of laravel-role-permission with dependencies
illuminate/auth Version ^8.12|^9.0|^10.0|^11.0
illuminate/container Version ^8.12|^9.0|^10.0|^11.0
illuminate/contracts Version ^8.12|^9.0|^10.0|^11.0
illuminate/database Version ^8.12|^9.0|^10.0|^11.0