Installation:
composer require beartropy/permissions
Publish the package assets and config:
php artisan vendor:publish --provider="Beartropy\Permissions\PermissionsServiceProvider"
Run Migrations:
php artisan migrate
(Ensure spatie/laravel-permission migrations have run first.)
Register Routes:
Add the middleware and routes to routes/web.php:
Route::middleware(['auth', 'verified'])->group(function () {
require __DIR__ . '/permissions.php';
});
First Use Case:
Visit /permissions (or your configured route) to access the dashboard. Authenticate as an admin to manage roles/permissions.
Role Management:
Permission Management:
posts.*, users.*) via the "Permission Groups" tab.Permission::findOrCreate() in your models to auto-generate permissions (e.g., Post::class . '.edit').User Assignment:
$user->givePermissionTo('edit-posts');
$user->assignRole('editor');
Livewire Components:
The UI is built with Livewire. Extend functionality by creating custom Livewire components that interact with the package’s models (Role, Permission, User).
Example:
use Beartropy\Permissions\Models\Role;
use Livewire\Component;
class CustomRoleManager extends Component {
public function syncPermissions($roleId, $permissionIds) {
$role = Role::findOrFail($roleId);
$role->syncPermissions($permissionIds);
}
}
Blade Directives: Use Spatie’s directives in your views for conditional rendering:
@can('edit-posts')
<button>Edit Post</button>
@endcan
API Endpoints: Expose the UI’s functionality via API routes for headless applications:
Route::get('/api/permissions/roles', [RoleController::class, 'index']);
Missing Spatie Dependency:
Class 'Spatie\Permission\Permission' not found.spatie/laravel-permission is installed and configured before using this package.Guard Mismatch:
guard name in the UI matches your Auth::guard() configuration (default: web).Livewire Caching:
php artisan cache:clear
php artisan view:clear
Permission Groups:
posts.*), but Spatie’s Permission::create() requires exact names.Log Permissions: Dump permissions for a role/user to debug:
dd(auth()->user()->getAllPermissions());
dd(\App\Models\Role::find(1)->permissions);
Check Middleware:
Ensure your admin routes use the correct middleware (e.g., auth, verified, and optionally can:admin).
Custom Fields:
Extend the Role or Permission models to add custom attributes (e.g., description, priority):
use Beartropy\Permissions\Models\Role as BaseRole;
class Role extends BaseRole {
protected $casts = [
'description' => 'string',
];
}
Update the UI via Livewire property binding.
Theming: Override the package’s Tailwind CSS by publishing the assets:
php artisan vendor:publish --tag=permissions-assets
Modify resources/css/permissions.css.
Audit Logging:
Integrate with spatie/laravel-activitylog to track role/permission changes:
use Spatie\Activitylog\LogOptions;
$role->logActivity('updated', $options = new LogOptions());
Multi-Guard Support:
Configure multiple guards in config/auth.php and extend the UI to toggle between them (requires custom Livewire logic).
Seed Data: Use Laravel’s seeder to pre-populate roles/permissions:
use Beartropy\Permissions\Models\Role;
use Beartropy\Permissions\Models\Permission;
public function run() {
$admin = Role::create(['name' => 'admin', 'guard_name' => 'web']);
Permission::create(['name' => 'access-admin', 'guard_name' => 'web']);
$admin->givePermissionTo('access-admin');
}
Soft Deletes:
Enable soft deletes for roles/permissions in the PermissionsServiceProvider:
use Illuminate\Database\Eloquent\SoftDeletes;
class Role extends Model {
use SoftDeletes;
}
Performance: For large-scale applications, eager-load relationships in the UI:
$roles = Role::with('permissions', 'users')->get();
How can I help you explore Laravel packages today?