组件
在 Blade 视图中渲染 Schema
NOTE
继续之前,请确保项目中已经安装了 filament/schemas。你可以运行如下命令检查是否已安装:
composer show filament/schemas
如果还未安装,请转到安装指南,并根据指示配置单个组件。
设置 Livewire 组件
首先,生成新的 Livewire 组件:
php artisan make:livewire ViewProduct
然后,在页面上渲染该 Livewire 组件:
@livewire('view-product')
或者,你也可以使用全页面 Livewire 组件:
use App\Livewire\ViewProduct;
use Illuminate\Support\Facades\Route;
Route::get('products/{product}', ViewProduct::class);
你必须在 Livewire 组件类中使用 InteractsWithSchemas trait 并实现 HasSchemas 接口:
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Concerns\RestrictsFileUploadsToSchemaComponents;
use Filament\Schemas\Contracts\HasSchemas;
use Livewire\Component;
class ViewProduct extends Component implements HasSchemas
{
use InteractsWithSchemas;
use RestrictsFileUploadsToSchemaComponents;
// ...
}
添加该 Schema
然后,请添加一个接受 $schema 对象的方法到 Livewire 组件,修改并返回它:
use Filament\Schemas\Schema;
public function productSchema(Schema $schema): Schema
{
return $schema
->components([
// ...
]);
}
最后,在 Livewire 组件视图中渲染该 Schema:
{{ $this->productSchema }}
NOTE
filament/schemas 也包含以下这些包:
filament/actionsfilament/support
这些包允许你在 Livewire 组件中使用它们的组件。
比如,如果你的 Schema 中使用了 Action,请记住实现 HasActions 接口并在 Livewire 组件类中使用 InteractsWithActions trait。
如果你在 Schema 中使用其他 Filament 组件,请确保同时安装并集成对应的包。
文件上传的安全注意事项
InteractsWithSchemas trait 会向所有使用它的组件暴露 Livewire 的文件上传 RPC 方法——无论该组件的 schema 中是否包含上传字段。如果你的 Livewire 组件可被不应上传任意文件的用户访问,请添加 RestrictsFileUploadsToSchemaComponents trait。详情请参阅 将 Livewire 文件上传限制为仅限 schema 组件。
Still need help? Join our Discord community or open a GitHub discussion