DEV Community

Agus Sudarmanto
Agus Sudarmanto

Posted on • Edited on

Filament Shield

Install Laravel

composer create-project laravel/laravel backend
composer require filament/filament:"^5.0"
php artisan filament:install --panels
Enter fullscreen mode Exit fullscreen mode

Install Filament Shield

composer require bezhansalleh/filament-shield
php artisan vendor:publish --tag="filament-shield-config"
Enter fullscreen mode Exit fullscreen mode

Set Filament Shield config file /config/filament-shield.php

'auth_provider_model' => 'App\\Models\\User',
Enter fullscreen mode Exit fullscreen mode

Set user model app/Models/User.php

use Spatie\Permission\Traits\HasRoles;

class User extends Authenticable
{
   use HasRoles;
}
Enter fullscreen mode Exit fullscreen mode

Run setup

php artisan shield:setup

# multi-tenancy
php artisan shield:setup --tenant=App\\Models\\Team
Enter fullscreen mode Exit fullscreen mode

Install Shield to Filament panel

php artisan shield:install admin --tenant --generate-relationships
# Replace 'admin' with your panel ID
Enter fullscreen mode Exit fullscreen mode

Command steps

Team is my tenant table, you could name it Organization, Tenant, etc.

composer require bezhansalleh/filament-shield
php artisan vendor:publish --tag="filament-shield-config"
php artisan shield:setup --tenant=App\\Models\\Team --force
php artisan shield:install --tenant
php artisan shield:super-admin --tenant=1
php artisan shield:generate --all --option=policies --relationships
php artisan shield:seeder
php artisan db:seed --class=ShieldSeeder
php artisan migrate --seed
php artisan shield:super-admin --user=1 --panel=board --tenant=App\\Models\\Team
php artisan make:filament-resource User
php artisan shield:generate --resource=UserResource --option=policies --relationships
Enter fullscreen mode Exit fullscreen mode

Don't forget to add tenant ownership relationship name on every filament resources.

<?php
use ...

class ....Resource extends Resource
{
    ...
    protected static ?string $tenantOwnershipRelationshipName = "teams";
    ...
}

Enter fullscreen mode Exit fullscreen mode

To exclude Tenant in certain resource add $isScopedToTenant = false.

<?php
...
...

class HelpdeskResource extends Resource
{
    protected static bool $isScopedToTenant = false;
Enter fullscreen mode Exit fullscreen mode

Generate

1. Generate Resource

Use the --generate flag when creating a resource to automatically create form fields and table columns based on your database schema, including simple relationship fields if doctrine/dbal is installed.

composer require doctrine/dbal --dev

php artisan make:filament-resource {resource-name} --model-namespace="App\\Models\\{vendor-name}\\{model-name}" --generate

# example
php artisan make:filament-resource Peraturan --model-namespace="App\\Models\\Agussudarmanto\\Peraturan" --generate
Enter fullscreen mode Exit fullscreen mode

2. Generate Relation Manager

php artisan make:filament-relation-manager CategoryResource posts title
Enter fullscreen mode Exit fullscreen mode

CategoryResource is the name of the parent resource class.
posts is the name of the relationship method in your Category model.
title is the attribute used to identify related records in the manager's table.
This command generates a new class in App\Filament\Resources\CategoryResource\RelationManagers\PostsRelationManager.php.

use Filament\Forms\Components\Section;
use Filament\Forms\Components\Grid;
-- to
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
Enter fullscreen mode Exit fullscreen mode

Filament Shield

Top comments (0)