1. Go to this page and download the library: Download edulazaro/larallow 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/ */
edulazaro / larallow example snippets
use EduLazaro\Larallow\Concerns\HasPermissions;
use EduLazaro\Larallow\Concerns\HasRoles;
class User extends Authenticatable
{
use HasPermissions, HasRoles;
}
// Assign a permission
$user->allow('edit-post');
// Or using the permissions() method
$user->permissions('edit-post')->allow();
// Check if user has permission (direct or via role)
$user->permissions('edit-post')->check();
// Check direct permission only
$user->hasPermission('edit-post');
use EduLazaro\Larallow\Models\Role;
// Create a role
$role = Role::create(['handle' => 'editor', 'name' => 'Editor']);
// Add permissions to the role
$role->addPermission('edit-post');
// Assign role to user
$user->assignRole($role);
// Check role
$user->hasRole('editor');
$user->assignRole($manager, $acme); // manager at Acme
$user->assignRole($viewer, $globex); // viewer at Globex
$user->hasRole('manager', $acme); // true
$user->hasRole('manager', $globex); // false
$user->allow('manage_clients', $acme); // only for Acme
$user->allow('manage_clients', $office); // only for that office
use EduLazaro\Larallow\Concerns\HasPermissions;
use EduLazaro\Larallow\Concerns\HasRoles;
class User extends Authenticatable
{
use HasPermissions, HasRoles;
}
use EduLazaro\Larallow\Concerns\IsRoleTenant;
class Account extends Model
{
use IsRoleTenant;
}
$roles = $account->roles()->get();
use Illuminate\Database\Eloquent\Relations\Relation;
// In AppServiceProvider boot()
Relation::morphMap([
'user' => User::class,
'client' => Client::class,
'office' => Office::class,
]);
$permissions = $user->permissions; // Collection of ActorPermission models
foreach ($permissions as $permission) {
echo $permission->permission; // string permission name
}
// Query specific permissions
$canEdit = $user->permissions()->where('permission', 'edit-post')->exists();
// Model method (simplest)
$user->permissions('edit-post')->check();
// With scope
$user->permissions('edit-post')->on($office)->check();
// Helper function
permissions('edit-post')->for($user)->check();
// Permissions class (verbose)
Permissions::query()
->permissions('edit-post')
->for($user)
->check();
// True if user has ANY of these permissions
$user->permissions(['edit-post', 'delete-post'])->check();
// True only if user has ALL permissions
$user->permissions(['edit-post', 'delete-post'])->checkAll();
// With scope
$user->permissions(['edit-post', 'delete-post'])->on($office)->checkAll();
$roles = $user->roles; // Collection of Role models
foreach ($roles as $role) {
echo $role->handle;
}
// Any of these roles
$user->roles(['editor', 'admin'])->check();
// All of these roles
$user->roles(['editor', 'admin'])->checkAll();
// With scope
$user->roles(['editor', 'admin'])->on($scope)->checkAll();
// Using helper
roles(['editor', 'admin'])->for($user)->check();
// Using Roles class (verbose)
Roles::query()
->roles(['editor', 'admin'])
->for($user)
->check();
@roles(['admin', 'editor'])
<p>You have elevated access.</p>
@endroles
@allroles(['admin', 'editor'])
<p>You have full admin/editor access.</p>
@endallroles
// Single permission
$user->hasRolePermission('edit-post');
$user->hasRolePermission('edit-post', $scope);
// All ;
$user->hasAnyRolePermissions(['edit-post', 'delete-post']);
// Users with a specific permission (direct or via role)
$users = User::withPermission('edit-post')->get();
$users = User::withPermission('edit-post', $office)->get();
// Users with any of these permissions
$users = User::withAnyPermission(['edit-post', 'delete-post'])->get();
// Users with all of these permissions
$users = User::withAllPermissions(['edit-post', 'delete-post'])->get();
// Get all permissions
$allPermissions = Permission::all();
// Get by handle
$permission = Permission::get('edit-post');
// Check existence
Permission::exists('edit-post');
// Check if allowed for actor/scope
Permission::isAllowedFor('edit-post', 'user', 'office');
$permissions = Permission::query()
->where('actor_type', 'user')
->where('scope_type', 'office')
->get();
// Alternative methods
$permissions = Permission::query()
->whereActorType('user')
->whereScopeType('office')
->get();
// Get as options array [handle => label]
$options = Permission::query()
->whereActorType('user')
->options();
// Pluck specific fields
$options = Permission::query()
->where('actor_type', 'user')
->pluck('label', 'handle')
->toArray();
// Filter by multiple handles
$permissions = Permission::query()
->where('handle', ['edit-post', 'delete-post'])
->get();
// Get first match
$permission = Permission::query()
->whereActorType('user')
->first();
// Short form syntax
$permissions = Permission::where('actor_type', 'user')
->where('scope_type', 'office')
->get();
$permission = Permission::where('actor_type', 'user')->first();
// Get translated name (uses current locale)
echo $role->name;
// Get specific locale with fallback
$name = $role->getTranslation('name', 'es', 'Default Name');
// Set translation
$role->setTranslation('name', 'fr', 'Nom en Français');
$role->save();