PHP code example of rockbuzz / lara-rbac

1. Go to this page and download the library: Download rockbuzz/lara-rbac 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/ */

    

rockbuzz / lara-rbac example snippets


use Rockbuzz\LaraRbac\Traits\HasRbac;
use Rockbuzz\LaraRbac\Contracts\HasRbac as RbacInterface;
	
class User extends Authenticatable implements RbacInterface
{
    use HasRbac;
    ...
	    
}

$adminRole = new \Rockbuzz\LaraRbac\Models\Role;
$adminRole->name = 'admin';
$adminRole->save();

$writerRole = new \Rockbuzz\LaraRbac\Models\Role;
$writerRole->name = 'writer';
$writerRole->save();

$user = User::find(1);
$user->attachRole($adminRole, $resource);

$user->attachRole($adminRole->id, $resource);

$user->attachRole([$adminRole->id, $writerRole->id], $resource);

$user->syncRoles([$adminRole->id, $writerRole->id], $resource);

$user->detachRoles([$adminRole, $writerRole->id], $resource);

$postStore = new \Rockbuzz\LaraRbac\Models\Permission;
$postStore->name = 'post.store';
$postStore->save();

$postUpdate = new \Rockbuzz\LaraRbac\Models\Permission;
$postUpdate->name = 'post.update';
$postUpdate->save();

$user = User::find(1);
$user->attachPermission($postStore, $resource);

$user->attachPermission($postStore->id, $resource);

$user->attachPermission([$postStore->id, $postUpdate->id], $resource);

$user->syncPermissions([$postStore->id, $postUpdate->id], $resource);

$user->detachPermissions([$postStore->id, $postUpdate->id], $resource);

auth()->user()->hasRole('admin', $resource);
auth()->user()->hasRole('admin|writer', $resource);
auth()->user()->hasPermission('post.update', $resource);
auth()->user()->hasPermission('post.update|delete.post', $resource);

$ php artisan migrate