codewithdennis/filament-factory-action

此插件为 Filament 管理面板表格添加了新功能,使您可以使用 Laravel 工厂定义轻松生成数据库表的测试记录。

v3.0.2 2024-04-01 09:32 UTC

This package is auto-updated.

Last update: 2024-08-31 00:44:07 UTC


README

Latest Version on Packagist Total Downloads

Filament Factory Action

此插件为 Filament 管理面板表格添加了新功能,使您可以使用 Laravel 工厂定义轻松生成数据库表的测试记录。

此插件扩展了标准 Filament 动作,确保您可以继续使用动作类中通常可用的所有方法

安装

您可以通过 composer 安装此包

composer require codewithdennis/filament-factory-action

使用示例

在利用此动作之前,确保您已经为您的模型设置了一个 工厂

class ProfileFactory extends Factory
{
    public function definition(): array
    {
        return [
            'name' => fake()->company(),
            'is_public' => rand(0, 1),
        ];
    }
}

假设您已经在 Filament 框架中有一个 ProfileResource。您可以将此动作集成到 ListProfiles 类中,如下所示。

FactoryAction::make(),
use App\Filament\Resources\ProfileResource;
use CodeWithDennis\FactoryAction\FactoryAction;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListProfiles extends ListRecords
{
    protected static string $resource = ProfileResource::class;

    protected function getHeaderActions(): array
    {
        return [
            FactoryAction::make()
                ->color('danger')
                // ->slideOver()
                ->icon('heroicon-o-wrench'),
            Actions\CreateAction::make()
        ];
    }
}

您可以使用以下示例创建/附加关联记录。请确保这些模型也具有相应的工厂。

protected function getHeaderActions(): array
{
    return [
        FactoryAction::make()
            // If you want to create or create and attach you can do so using `hasMany`
            ->hasMany([Badge::class, Category::class])
            // If you want to attach existing models you can do so using `belongsToMany`
            ->belongsToMany([Badge::class, Category::class]),
    ];
}

默认行为是在生产环境中隐藏此动作,但您可以使用自己的逻辑覆盖此行为。

->hidden(fn() => false)

展示

example-1 example-2 example-3

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查看我们关于如何报告安全漏洞的 安全策略

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件