PHP code example of genealabs / laravel-governor

1. Go to this page and download the library: Download genealabs/laravel-governor 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/ */

    

genealabs / laravel-governor example snippets


    use GeneaLabs\LaravelGovernor\Traits\Governing;

    class User extends Authenticatable
    {
        use Governing;
    }
    

    use GeneaLabs\LaravelGovernor\Traits\Governable;

    class BlogPost extends Model
    {
        use Governable;
    }
    

'cache' => [
    'enabled' => true,
    'ttl' => 3600, // seconds, or null for "forever"
],

<li><a href="{{ route('genealabs.laravel-governor.roles.index') }}">Governor</a></li>

if ($user->hasRole('Editor')) {
    // user has the Editor role
}

$roleNames = $user->roles->pluck('name'); // ['SuperAdmin', 'Member']

$myTeams = $user->ownedTeams;

$teamNames = $user->teams->pluck('name');

$permissions = $user->permissions;

$effective = $user->effectivePermissions;

$owner = $blogPost->ownedBy;

$teams = $blogPost->teams;

use Laravel\Nova\Resource as NovaResource;
use Laravel\Nova\Http\Requests\NovaRequest;

abstract class Resource extends NovaResource
{
    public static function indexQuery(NovaRequest $request, $query)
    {
        return $query->viewAnyable();
    }
}

class BlogPostPolicy extends BasePolicy
{
    public function publish(?Model $user, Model $model): bool
    {
        return $this->authorizeCustomAction($user, $model);
    }

    public function archive(?Model $user, Model $model): bool
    {
        return $this->authorizeCustomAction($user, $model);
    }
}

$user->can('publish', $blogPost);



namespace App\Policies;

use GeneaLabs\LaravelGovernor\Policies\BasePolicy;

class BlogPostPolicy extends BasePolicy
{
    // All standard CRUD permissions are handled automatically.
    // Add custom methods only for non-standard actions.
}

// In controllers
$this->authorize('update', $blogPost);

// In Blade templates
@can('delete', $blogPost)
    <button>Delete</button>
@endcan

// Directly on the user
$user->can('create', App\Models\BlogPost::class);
$user->can('update', $blogPost);

// Can the user create a Role?
GET /api/genealabs/laravel-governor/user-can/create?model=GeneaLabs\LaravelGovernor\Role

// Can the user update Role with primary key 1?
GET /api/genealabs/laravel-governor/user-can/update?model=GeneaLabs\LaravelGovernor\Role&primary-key=1

GET /api/genealabs/laravel-governor/user-is/SuperAdmin



return [
    'layout-view' => 'layouts.app',
    'content-section' => 'content',
    'auth-model-primary-key-type' => 'bigInteger',
    'models' => [
        'auth' => config('auth.providers.users.model') ?? config('auth.model'),
        'action' => GeneaLabs\LaravelGovernor\Action::class,
        'assignment' => GeneaLabs\LaravelGovernor\Assignment::class,
        'entity' => GeneaLabs\LaravelGovernor\Entity::class,
        'group' => GeneaLabs\LaravelGovernor\Group::class,
        'ownership' => GeneaLabs\LaravelGovernor\Ownership::class,
        'permission' => GeneaLabs\LaravelGovernor\Permission::class,
        'role' => GeneaLabs\LaravelGovernor\Role::class,
        'team' => GeneaLabs\LaravelGovernor\Team::class,
        'invitation' => GeneaLabs\LaravelGovernor\TeamInvitation::class,
    ],
    'user-name-property' => 'name',
    'url-prefix' => '/genealabs/laravel-governor/',
    'superadmins' => env('GOVERNOR_SUPERADMINS'),
    'admins' => env('GOVERNOR_ADMINS'),
    'entity-aliases' => [],
    'cache' => [
        'enabled' => false,
        'ttl' => 3600,
    ],
];



namespace App\Policies;

use GeneaLabs\LaravelGovernor\Policies\BasePolicy;
use Illuminate\Database\Eloquent\Model;

class BlogPostPolicy extends BasePolicy
{
    // Standard CRUD actions are inherited automatically.

    // Custom action: publish
    public function publish(?Model $user, Model $model): bool
    {
        return $this->authorizeCustomAction($user, $model);
    }

    // Custom action: archive
    public function archive(?Model $user, Model $model): bool
    {
        return $this->authorizeCustomAction($user, $model);
    }
}

// Get only blog posts the user can view
$posts = BlogPost::viewable()->paginate(15);

// Get only blog posts the user can edit
$editable = BlogPost::updatable()->get();

// Get only blog posts the user can delete
$deletable = BlogPost::deletable()->get();

// Check if user has a specific role
if ($user->hasRole('Editor')) {
    // ...
}

// Check policy authorization (standard Laravel)
if ($user->can('update', $blogPost)) {
    // ...
}

// Check custom action
if ($user->can('publish', $blogPost)) {
    // ...
}

// Get all effective permissions for a user
$permissions = $user->effectivePermissions;

// Get user's teams
$teams = $user->teams;

// Get teams owned by the user
$ownedTeams = $user->ownedTeams;

// Get team members
$members = $team->members;

// Transfer team ownership (via web route)
POST /genealabs/laravel-governor/teams/{team}/transfer-ownership
sh
    php artisan migrate --path="vendor/genealabs/laravel-governor/database/migrations"
    php artisan db:seed --class=LaravelGovernorDatabaseSeeder
    
sh
    php artisan db:seed
    
sh
    php artisan db:seed --class=LaravelGovernorPermissionsTableSeeder
    
sh
    php artisan governor:publish --assets
    
sh
php artisan db:seed --class="LaravelGovernorUpgradeTo0120"
sh
php artisan db:seed --class="LaravelGovernorUpgradeTo0115"
sh
    php artisan db:seed --class="LaravelGovernorUpgradeTo0110"
    
sh
php artisan migrate --path="vendor/genealabs/laravel-governor/database/migrations"
php artisan db:seed --class="LaravelGovernorDatabaseSeeder"
php artisan db:seed --class="LaravelGovernorUpgradeTo0100"
sh
php artisan governor:publish --config
sh
php artisan governor:publish --views
sh
php artisan governor:publish --assets    # Publish frontend assets
php artisan governor:publish --config    # Publish config file
php artisan governor:publish --views     # Publish Blade views
php artisan governor:publish --migrations # Publish migration files
sh
php artisan governor:publish --config --views
sh
php artisan governor:setup [email protected]
php artisan governor:setup --user=1