本文目录导读:

我来为你详细介绍 Filament——这个目前最受欢迎的 Laravel 管理面板解决方案。
Filament 是一套为 Laravel 构建的全栈组件集合,可以快速构建功能丰富的管理面板、仪表盘和 SaaS 应用。
核心特征
TALL 技术栈
- Tailwind CSS (样式)
- Alpine.js (前端交互)
- Laravel (后端框架)
- Livewire (动态组件)
主要组件
- Panel Builder - 完整的管理面板构建器
- Table Builder - 数据表格构建器
- Form Builder - 表单构建器
- Notifications - 通知系统
- Actions - 操作按钮
- Infolists - 信息展示列表
- Widgets - 仪表盘小部件
快速入门示例
安装
composer require filament/filament:"^3.0" php artisan filament:install --panels php artisan make:filament-user
创建资源(CRUD)
php artisan make:filament-resource Product
生成的资源文件
// app/Filament/Resources/ProductResource.php
namespace App\Filament\Resources;
use App\Filament\Resources\ProductResource\Pages;
use App\Models\Product;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
class ProductResource extends Resource
{
protected static ?string $model = Product::class;
protected static ?string $navigationIcon = 'heroicon-o-shopping-bag';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('price')
->required()
->numeric()
->prefix('$'),
Forms\Components\Select::make('category_id')
->relationship('category', 'name')
->required(),
Forms\Components\RichEditor::make('description')
->columnSpanFull(),
Forms\Components\Toggle::make('is_active')
->required(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('price')
->money('USD')
->sortable(),
Tables\Columns\TextColumn::make('category.name')
->sortable(),
Tables\Columns\IconColumn::make('is_active')
->boolean(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
Tables\Filters\SelectFilter::make('category')
->relationship('category', 'name'),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListProducts::route('/'),
'create' => Pages\CreateProduct::route('/create'),
'edit' => Pages\EditProduct::route('/{record}/edit'),
];
}
}
高级功能
自定义仪表盘
// app/Filament/Pages/Dashboard.php
class Dashboard extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-home';
protected static string $view = 'filament.pages.dashboard';
protected function getWidgets(): array
{
return [
StatsOverviewWidget::class,
LatestOrdersWidget::class,
RevenueChartWidget::class,
];
}
}
// 统计小部件
class StatsOverviewWidget extends StatsOverviewWidget
{
protected function getCards(): array
{
return [
Stat::make('Total Users', User::count())
->description('32k increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->chart([7, 2, 10, 3, 15, 4, 17])
->color('success'),
Stat::make('Bounce rate', '21%')
->description('7% increase')
->descriptionIcon('heroicon-m-arrow-trending-down')
->color('danger'),
Stat::make('Average time on page', '3:12')
->description('3% increase')
->descriptionIcon('heroicon-m-arrow-trending-up')
->color('warning'),
];
}
}
自定义表单字段
Forms\Components\Builder::make('content')
->blocks([
Builder\Block::make('heading')
->schema([
TextInput::make('content')
->label('Heading text')
->required(),
Select::make('level')
->options([
'h1' => 'Heading 1',
'h2' => 'Heading 2',
'h3' => 'Heading 3',
])
->required(),
]),
Builder\Block::make('paragraph')
->schema([
RichEditor::make('content')
->label('Paragraph text')
->required(),
]),
])
权限与角色
// config/filament.php
'auth' => [
'guard' => 'web',
'pages' => [
'login' => \Filament\Http\Livewire\Auth\Login::class,
],
],
// 在资源中配置权限
public static function canViewAny(): bool
{
return auth()->user()->can('view_products');
}
public static function canCreate(): bool
{
return auth()->user()->can('create_products');
}
主题定制
npm install tailwindcss @tailwindcss/forms @tailwindcss/typography
// resources/css/filament/admin.css
@import '../../../../vendor/filament/filament/resources/css/app.css';
// 自定义样式
.fi-btn-primary {
@apply bg-gradient-to-r from-blue-500 to-purple-600;
}
最佳实践
性能优化
// 使用 lazy 加载关系
Tables\Columns\TextColumn::make('orders.total')
->lazy()
// 使用 withCount
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('orders_count')
->counts('orders'),
]);
}
表单验证
Forms\Components\TextInput::make('email')
->email()
->unique(ignoreRecord: true)
->required()
->rule(function () {
return function (string $attribute, $value, Closure $fail) {
if (!str_ends_with($value, '@company.com')) {
$fail('The :attribute must be a company email.');
}
};
});
多面板支持
php artisan filament:panel admin php artisan filament:panel staff
// app/Providers/Filament/AdminPanelProvider.php
class AdminPanelProvider extends PanelProvider
{
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->login()
->pages([
Pages\Dashboard::class,
])
->resources([
ProductResource::class,
UserResource::class,
]);
}
}
国际化
// config/filament.php
'languages' => [
'en' => 'English',
'zh_CN' => '中文',
],
// 使用翻译
Forms\Components\TextInput::make('name')
->label(__('fields.name'))
常见问题解决
性能问题
// 减少查询
public static function table(Table $table): Table
{
return $table
->query(Product::query()->with('category'))
->paginated([10, 25, 50]);
}
// 使用 eager loading
ProductResource::table()
->columns([
Tables\Columns\TextColumn::make('category.name')
->eagerLoad(),
]);
自定义布局
// 创建自定义页面
class CustomPage extends Page
{
protected static string $view = 'custom-page';
public function getHeader(): ?string
{
return view('custom-header', [
'title' => 'Custom Page',
]);
}
}
生态系统
- Filament Shield - 权限管理
- Filament Spatie Media Library - 媒体管理
- Filament Excel - Excel 导入导出
- Filament Breezy - 用户管理
- Filament Marketplace - 插件市场
Filament 的优势:
- ✅ 快速构建:减少 70% 的开发时间
- ✅ 现代 UI:基于 Tailwind CSS
- ✅ 完整的生态系统
- ✅ 活跃的社区
- ✅ 良好的文档
- ✅ 企业级功能
相比传统的 Laravel 管理面板,Filament 提供了更好的开发体验和用户体验,它的组件化设计和 Livewire 的结合使其既灵活又高效。
如果你要开始新的 Laravel 管理面板项目,Filament 是目前最推荐的选择,需要深入了解某个特定功能吗?