codewithdennis/filament-factory-action
Installation:
composer require codewithdennis/filament-factory-action
Publish the config (if needed):
php artisan vendor:publish --provider="CodeWithDennis\FilamentFactoryAction\FilamentFactoryActionServiceProvider"
Register the Action:
Add the action to your FilamentResource (e.g., ProfileResource) via the getTableActions() method:
use CodeWithDennis\FilamentFactoryAction\Actions\FactoryAction;
public static function getTableActions(): array
{
return [
FactoryAction::make(),
];
}
First Use Case:
/admin/profiles).database/factories/. Example:
// database/factories/ProfileFactory.php
public function definition(): array
{
return [
'name' => fake()->name(),
'is_public' => fake()->boolean(),
];
}
getTableActions() method in your FilamentResource to register the action.config/filament-factory-action.php for default settings (e.g., modal title, button label).Basic Integration:
// ProfileResource.php
public static function getTableActions(Profile $record): array
{
return [
FactoryAction::make()
->label('Create Test Profile')
->modalHeading('Generate Test Data'),
];
}
Dynamic Factory Selection:
Use the factory() method to specify a custom factory class or state:
FactoryAction::make()
->factory(ProfileFactory::new())
->state(fn () => ['is_public' => true]), // Override factory attributes
Bulk Generation: Combine with Filament’s bulk actions for multi-record creation:
public static function getTableActions(): array
{
return [
FactoryAction::make()
->label('Generate 5 Test Records')
->quantity(5), // Default: 1
];
}
Conditional Visibility: Hide the action based on logic (e.g., environment):
FactoryAction::make()
->visible(app()->environment('local')),
Test Data Setup:
User records with varied roles for testing permissions.Demo Environments:
Debugging:
Filament Policies: Restrict access to the action using Filament’s built-in policies:
FactoryAction::make()
->visible(fn () => auth()->user()->can('generate-test-data')),
Event Listeners:
Trigger custom logic after record creation via the afterCreate callback:
FactoryAction::make()
->afterCreate(fn (Profile $profile) => Log::info("Created test profile: {$profile->id}")),
Factory States: Leverage factory states for reusable test scenarios:
// ProfileFactory.php
public function withAdmin(): self
{
return $this->state(['role' => 'admin']);
}
// Resource.php
FactoryAction::make()
->factory(ProfileFactory::new()->withAdmin()),
Factory Not Found:
Factory [Model] not defined.ProfileFactory) and is registered in AppServiceProvider@registerFactories.Mass Assignment Risks:
$fillable or $guarded.$fillable in your model or use ->state() to whitelist attributes.Modal Overlaps:
config/filament-factory-action.php:
'modal_z_index' => 50,
Performance:
quantity() sparingly or batch creation via queues:
FactoryAction::make()
->quantity(1000)
->dispatchNow(), // Use Laravel queues for large batches
Log Factory Output:
Enable debug logging in config/filament-factory-action.php:
'debug' => env('APP_DEBUG', false),
Check logs for generated attributes:
tail -f storage/logs/laravel.log
Validate Factory State:
Use dd() in the factory’s definition() method to inspect generated data:
public function definition(): array
{
$data = [
'name' => fake()->name(),
// ...
];
dd($data); // Debug output
return $data;
}
Check for Conflicts:
spatie/laravel-filament-actions).php artisan view:clear
Custom Icons: Replace the default icon with a custom SVG:
FactoryAction::make()
->icon(SvgIcon::make('heroicon-o-plus-circle')->size(20)),
Localization: Translate labels/modals using Filament’s localization:
FactoryAction::make()
->label(__('filament-factory-action::actions.create'))
->modalHeading(__('filament-factory-action::modals.heading')),
Environment-Specific Factories: Dynamically switch factories based on environment:
FactoryAction::make()
->factory(app()->environment('staging') ? StagingProfileFactory::new() : ProfileFactory::new()),
Extend the Action:
Create a custom action class by extending FactoryAction:
namespace App\Actions;
use CodeWithDennis\FilamentFactoryAction\Actions\FactoryAction;
class CustomFactoryAction extends FactoryAction
{
protected string $modalHeading = 'Custom Test Data';
// Override methods as needed
}
API Integration: Expose the action via Filament’s API for programmatic use:
// routes/api.php
Route::post('/admin/profiles/factory', [ProfileResource::class, 'factoryAction'])->name('filament.admin.resources.profiles.factory-action');
How can I help you explore Laravel packages today?