1. Go to this page and download the library: Download obrainwave/access-tree 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/ */
obrainwave / access-tree example snippets
use Obrainwave\AccessTree\Traits\HasRole;
class User extends Authenticatable
{
use HasRole;
}
return [
// Basic Configuration
'seed_permissions' => true,
'seed_roles' => true,
'assign_first_user_as_admin' => true,
'cache_refresh_time' => 5,
'forbidden_redirect' => 'home',
// User Model Configuration
'user_model' => 'App\\Models\\User',
// Admin Interface Configuration
'admin_favicon' => null, // Path to custom favicon
// Universal Table Management
'managed_tables' => [], // Empty = all tables, or specify: ['posts', 'products', 'orders']
'dashboard_table_cards' => [], // Which managed tables show cards on dashboard
// Styling Configuration
'styling' => [
'framework' => 'bootstrap', // bootstrap, tailwind, or custom
'theme' => 'modern', // modern, classic, or minimal
'dark_mode' => false, // Enable dark mode by default
'animations' => true, // Enable animations
'custom_css' => null, // Custom CSS string
],
];
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Obrainwave\AccessTree\Traits\HasRole;
class User extends Authenticatable
{
use HasRole;
}
use Obrainwave\AccessTree\Facades\AccessTree; or use AccessTree;
// Create permission
AccessTree::createAccess([
'name' => 'Edit Articles',
'status' => 1
], 'Permission');
// Create role with permissions
AccessTree::createAccess([
'name' => 'Editor',
'status' => 1
], 'Role', [1, 2, 3]); // Permission IDs
$user = User::find(1);
// Assign a single role
$user->assignRole('admin');
// Assign multiple roles
$user->assignRoles(['editor', 'moderator']);
// Sync roles (removes existing, adds new ones)
$user->syncRoles([1, 2, 3]);
// Using helper function
createUserRole([1, 2], $user->id);
// Check single permission
if (checkPermission('edit_articles')) {
// User has permission
}
// Check multiple permissions (strict - all at least one)
if (checkPermissions(['edit_articles', 'delete_articles'], false)) {
// User has AT LEAST ONE permission
}
// Check role
if (checkRole('admin')) {
// User has role
}
// Using model methods
if ($user->hasRole('admin')) {
// User is admin
}
if ($user->hasPermission('edit_articles')) {
// User can edit articles
}
@if(checkPermission('edit_articles'))
<button>Edit Article</button>
@endif
@if(checkRole('admin'))
<div class="admin-panel">Admin Controls</div>
@endif
@if(checkPermissions(['edit_articles', 'delete_articles'], true))
<div>User can both edit and delete articles</div>
@endif
// Mark user as root
$user = User::find(1);
$user->is_root_user = true;
$user->save();
// Check if current user is root
if (isRootUser()) {
// Bypasses all permission checks
}
// Check if specific user is root
if (isRootUser($user->id)) {
// User has full access
}
// Configure which tables to manage (in config/accesstree.php)
'managed_tables' => [
'posts',
'products',
'orders',
'categories'
], // Empty array = manage all tables
// config/accesstree.php
'managed_tables' => [
'posts',
'products',
'orders',
'user_wallet'
], // Only these tables will appear in sidebar
// Leave empty [] to manage all tables
// config/accesstree.php
'dashboard_table_cards' => [
'posts',
'products'
], // Only these will show as cards on dashboard
// Leave empty [] for no table cards
// Get all active permissions
$permissions = fetchActivePermissions();
// Get roles with pagination
$roles = fetchRoles([
'paginate' => true,
'per_page' => 15,
'with_relation' => true,
'order' => 'asc',
'order_ref' => 'name'
]);
// Get specific role with permissions
$role = fetchRole(1);
// Get user's roles
$userRoles = fetchUserRoles($user->id);
// Get role permissions
$rolePermissions = fetchRolePermissions($roleId);
public function test_admin_can_access_dashboard()
{
$user = User::factory()->create();
AccessTree::assignRoles($user->id, ['Admin']); // or assign role id
$this->actingAs($user)
->get('/admin')
->assertStatus(200);
}