Installation
composer require tomatophp/filament-accounts
Publish the package assets and config:
php artisan vendor:publish --provider="TomatoPHP\FilamentAccounts\FilamentAccountsServiceProvider" --tag="filament-accounts-config"
php artisan vendor:publish --provider="TomatoPHP\FilamentAccounts\FilamentAccountsServiceProvider" --tag="filament-accounts-migrations"
Run migrations:
php artisan migrate
Register the Plugin
Add to app/Providers/Filament/AdminPanelProvider.php:
public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\TomatoPHP\FilamentAccounts\FilamentAccountsPlugin::make(),
]);
}
First Use Case
Create a multi-auth system with a single users table:
// config/filament-accounts.php
'auth' => [
'drivers' => [
'admin' => [
'guard' => 'admin',
'model' => \App\Models\User::class,
],
'client' => [
'guard' => 'client',
'model' => \App\Models\User::class,
],
],
],
Access the Filament panel (/admin/accounts) to manage accounts.
Account Creation Use the built-in Filament form to create accounts with multiple roles/guards:
// Example: Create an admin account
$account = \TomatoPHP\FilamentAccounts\Models\Account::create([
'email' => 'admin@example.com',
'password' => bcrypt('secure123'),
'accounts' => [
'admin' => true,
'client' => false,
],
]);
Switching Auth Guards Dynamically switch guards in middleware or controllers:
use TomatoPHP\FilamentAccounts\Facades\FilamentAccounts;
FilamentAccounts::setGuard('client'); // Switch to client guard
Integration with Filament Policies Extend Filament policies to restrict access per guard:
// app/Policies/AdminPolicy.php
public function viewAny(User $user)
{
return FilamentAccounts::hasGuard('admin');
}
Customizing Account Fields Override default fields via a service provider:
public function boot()
{
FilamentAccounts::extendAccountForm(function (Form $form) {
$form->schema([
// Custom fields here
]);
});
}
Multi-Guard Middleware
public function handle(Request $request, Closure $next)
{
if (!FilamentAccounts::hasGuard('admin')) {
abort(403);
}
return $next($request);
}
Seeding Multi-Accounts
// database/seeders/AccountSeeder.php
FilamentAccounts::createAccount([
'email' => 'superadmin@example.com',
'accounts' => [
'admin' => true,
'client' => true,
'superadmin' => true,
],
]);
API Integration
Use the FilamentAccounts facade to validate requests:
public function login(Request $request)
{
$guard = $request->input('guard', 'admin');
if (!FilamentAccounts::hasGuard($guard)) {
return response()->json(['error' => 'Invalid guard'], 400);
}
// Proceed with auth
}
Guard Mismatch
config/filament-accounts.php after adding new guards.php artisan config:clear or verify guards in the Filament UI under Settings > Accounts.Migration Conflicts
users table structure conflicts with package migrations.--force (backup first):
php artisan migrate --force
Caching Headaches
php artisan optimize:clear
php artisan view:clear
Permission Denied
config/filament.php for auth middleware and ensure guards are registered in AuthServiceProvider.Log Guard Switches
Enable debug mode in config/filament-accounts.php:
'debug' => env('APP_DEBUG', false),
Check logs for guard-related events.
Inspect Account Data Use Tinker to verify account data:
php artisan tinker
>>> \TomatoPHP\FilamentAccounts\Models\Account::first()->accounts
Filament Plugin Isolation If the plugin doesn’t appear, verify:
FilamentAccountsPlugin is registered in AdminPanelProvider.Custom Account Models Override the default model by binding it in a service provider:
FilamentAccounts::extendModel(\App\Models\CustomAccount::class);
Dynamic Guard Assignment Use events to assign guards dynamically:
FilamentAccounts::listen('account.created', function ($account) {
if ($account->email === 'admin@example.com') {
$account->assignGuard('superadmin');
}
});
API Resource Integration Extend the default API resource:
FilamentAccounts::extendApiResource(function (ApiResource $resource) {
$resource->schema([
// Custom API fields
]);
});
Localization Override translations:
// resources/lang/en/filament-accounts.php
return [
'accounts' => [
'list' => 'My Accounts',
],
];
How can I help you explore Laravel packages today?