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"
],
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
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"