最近一直在用 Laravel Filament 做项目,功能是真的强大,极大的提升了工作效率,因为也算是 Filament 新手,所以本文记录一些在使用过程中总结的小技巧、小心得。

统一时间格式

// Form
$setting = app(GeneralSettings::class);

DateTimePicker::configureUsing(function (DateTimePicker $datetime) use ($setting): void {
    $datetime->timezone($setting->timezone())
        ->format($setting->datetimeFormat());
});

// Table 

Table::$defaultCurrency = $this->settings->currency();
Table::$defaultDateDisplayFormat = $this->settings->dateFormat();
Table::$defaultTimeDisplayFormat = $this->settings->timeFormat();
Table::$defaultDateTimeDisplayFormat = $this->settings->datetimeFormat();

Logo 使用视图模版

$panel->brandLogo(
    fn () => view(
        LogoStyle::preview(
            // isRand: true // 可设置随机
            theme:LogoStyle::Deconstructed
        ),
        [
            'brandName' => $general->appName(),
            'primaryColor' => Color::Emerald
        ]
    )
)

底部版权视图

$panel->renderHook(
    'panels::footer',
    fn () => view('module-cores::layouts/footer')
);

通过中间件更改主题色

FilamentColor::register([
    'primary' => $this->settings->themeColor(),
]);

修改原 icon

FilamentIcon::register([
    'actions::create-action.grouped' => 'heroicon-o-plus',
    'actions::view-action.grouped' => 'heroicon-o-eye',
    'actions::delete-action.grouped' => 'heroicon-o-trash',
    'forms::components.repeater.actions.delete' => 'heroicon-o-trash',
    'tables::actions.filter' => 'heroicon-o-funnel',
    'tables::actions.toggle-columns' => 'heroicon-o-view-columns'
]);

让字段支持多语言

Field::macro('translatable', function (bool $translatable = true, ?array $customLocales = null, ?array $localeSpecificRules = null) use ($supportedLocales) {
            if (! $translatable) {
                return $this;
            }

            $field = $this->getClone();

            $tabs = collect($customLocales ?? $supportedLocales)
                ->map(function ($entry) use ($field, $localeSpecificRules) {
                    $locale = $entry->slug;

                    $clone = $field
                        ->getClone()
                        ->name("{$field->getName()}.{$locale}")
                        ->label($field->getLabel())
                        ->statePath("{$field->getStatePath(false)}.{$locale}");

                    if ($localeSpecificRules && isset($localeSpecificRules[$locale])) {
                        $clone->rules($localeSpecificRules[$locale]);
                    }

                    return TabsTab::make($locale)
                        ->label($entry->label)
                        ->schema([$clone]);
                })
                ->toArray();

            $tabsField = TranslateFormTags::make('translations')
                ->tabs($tabs);

            return $tabsField;
        });

使用

TextInput::make('name')->translatable();

效果

通过 theme.css 设置 Modal 背景模糊

.fi-modal-close-overlay{
    @apply backdrop-blur-sm;
}