PHP code example of erag / laravel-role-permission
1. Go to this page and download the library: Download erag/laravel-role-permission library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
erag / laravel-role-permission example snippets
namespace App\Models;
use EragPermission\Traits\HasPermissionsTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use HasFactory, HasPermissionsTrait, Notifiable;
}
// Single expiration for all permissions
$user->givePermissionsTo(['post-create', 'post-edit'], Carbon::now()->addDays(30));
// Different expirations per permission
$user->givePermissionsTo(['post-create', 'post-edit'], [
'post-create' => Carbon::now()->addDays(10),
'post-edit' => Carbon::now()->addHours(6)
]);
// No expiration
$user->givePermissionsTo(['post-create']);
if ($user->hasRole('admin', 'editor')) {
// User has at least one of these roles
}
if (hasPermissions('post-create')) {
dd('You are allowed to access');
} else {
dd('You are not allowed to access');
}
if (hasPermissions('post-create|post-edit')) {
dd('You are allowed to access');
} else {
dd('You are not allowed to access');
}
if (hasPermissions('post-create,post-edit')) {
dd('You are allowed to access');
} else {
dd('You are not allowed to access');
}
getPermissions();
getRoles();
if (hasRole('admin')) {
dd('You are allowed to access');
} else {
dd('You are not allowed to access');
}
Route::group(['middleware' => ['role:admin,post-create']], function () {
// Routes protected by role and permissions
});
Route::group(['middleware' => ['permissions:post-create']], function () {
// Routes protected by permissions
});
Route::post('/create-post', [PostController::class, 'create'])->name('post.create')->middleware('role:admin,post-create');
Route::post('/create-post', [PostController::class, 'create'])->name('post.create')->middleware('permissions:post-create');
// Assign a permission with a specific expiration date
$user->givePermissionsTo(['post-create', 'post-edit'],
Carbon::now()->addDays(30), // Each Permission expiration assign in 30 days
);
$user->givePermissionsTo(['post-create', 'post-edit'],
[
Carbon::now()->addDays(10), // Expires in 10 days
Carbon::now()->addHours(6), // Expires in 6 hours
]);
// Assign a permission with a specific expiration date
$user->givePermissionsTo(['post-create'],
null, // [] Array or String
);
blade
@role('admin')
<!-- Content for admins -->
@endrole
@hasPermissions('post-create')
<!-- Content for users with post-create permission -->
@endhasPermissions
@hasPermissions('post-create|post-edit')
<!-- Content for users with either permission -->
@endhasPermissions
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.