filamentphp

环境准备

安装 PHP 8.3

1
sudo apt install php8.3-cli php8.3-fpm php8.3-xml php8.3-mbstring php8.3-curl php8.3-gd php8.3-intl php8.3-zip php8.3-mysql

创建 laravel 项目

1
composer create-project laravel/laravel filament-test

切换到项目目录

1
cd filament-test

安装 filament

1
composer require filament/filament:"^3.2" -W

安装 panels

1
php artisan filament:install --panels
1
2
3
┌ What is the ID? ─────────────────────────────────────────────┐
admin
└──────────────────────────────────────────────────────────────┘
1
2
3
All done! Would you like to show some love by starring the Filament repo on GitHub? ┐
│ ○ Yes / ● No
└─────────────────────────────────────────────────────────────────────────────────────┘

配置

修改 .env 配置数据库

1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=filament-test
DB_USERNAME=demo
DB_PASSWORD=12345678

迁移数据库

1
php artisan migrate
1
2
3
Name ────────────────────────────────────────────────────────┐
│ demo │
└──────────────────────────────────────────────────────────────┘
1
2
3
4
┌ Email address ───────────────────────────────────────────────┐
│ demo@outlook.com │
└──────────────────────────────────────────────────────────────┘

1
2
3
Password ────────────────────────────────────────────────────┐
│ •••••••• │
└──────────────────────────────────────────────────────────────┘

启动项目

1
php artisan serve

后台访问地址

1
http://127.0.0.1:8000/admin/login

User

以用户为例, 创建 用户面板

1
php artisan make:filament-resource User

修改 文件 UserResource.php

1
INFO  Filament resource [app/Filament/Resources/UserResource.php] created successfully.

form 方法用来生成表单

1
2
3
4
5
6
7
public static function form(Form $form): Form
{
return $form
->schema([
//
]);
}

table 方法用来生成列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static function table(Table $table): Table
{
return $table
->columns([
//
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

添加用户

1
2
3
4
5
6
7
8
9
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name'),
Forms\Components\TextInput::make('email'),
Forms\Components\TextInput::make('password'),
]);
}

访问添加用户页面

1
http://127.0.0.1:8000/admin/users/create

默认使用自动填充数据, 默认模型不允许自动填充, 需要设置允许填充的字段, 修改用户模型 User

1
2
3
4
5
6
7
8
class User
{
protected $fillable = [
'name',
'email',
'password',
];
}

用户列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id'),
Tables\Columns\TextColumn::make('name'),
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('email_verified_at'),
Tables\Columns\TextColumn::make('created_at'),
Tables\Columns\TextColumn::make('updated_at'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

访问用户列表页面

1
http://127.0.0.1:8000/admin/users

filamentphp
http://example.com/2024/10/20/filamentphp/
作者
John Doe
发布于
2024年10月20日
许可协议