文件上传
简介
文件上传字段基于 Filepond。
use Filament\Forms\Components\FileUpload;
FileUpload::make('attachment')

TIP
Filament 也支持 spatie/laravel-medialibrary
。请查看插件文档以获取更多信息。
配置存储磁盘及目录
默认情况下,文件会上传到配置文件中定义的存储磁盘中。你也可以设置 FILESYSTEM_DISK
环境变量来修改他。
TIP
为了正确预览图像和其他文件,Filepond 要求文件与应用来自同一域名,或者需要存在相应的 CORS 标头。确保 APP_URL
环境变量正确,或修改文件系统驱动以设置正确的URL。如果你在单独的域名(如 S3)上托管文件,请确保设置了 CORS 标头。
要修改特定字段的磁盘和目录,以及文件的可见度,请使用 disk()
、directory()
和 visibility()
方法。默认情况下,文件以 private
可见度上传到你的存储磁盘,除非你将该磁盘设置为 public
:
use Filament\Forms\Components\FileUpload;
FileUpload::make('attachment')
->disk('s3')
->directory('form-attachments')
->visibility('public')
除了允许静态值之外,disk()
、directory()
和 visibility()
方法也接受函数来动态计算它们的值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.Utility | Type | Parameter | Description |
---|---|---|---|
Field | Filament\Forms\Components\Field | $component | The current field component instance. |
Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current form data. Validation is not run. |
Livewire | Livewire\Component | $livewire | The Livewire component instance. |
Eloquent model FQN | ?string<Illuminate\Database\Eloquent\Model> | $model | The Eloquent model FQN for the current schema. |
Operation | string | $operation | The current operation being performed by the schema. Usually <code>create</code>, <code>edit</code>, or <code>view</code>. |
Raw state | mixed | $rawState | The current value of the field, before state casts were applied. Validation is not run. |
Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
State | mixed | $state | The current value of the field. Validation is not run. |
NOTE
如果这些文件被移除,开发者有责任从磁盘中删除它们,因为 Filament 无法感知它们是否被其他地方依赖。自动执行此操作的一种方法是观察 模型事件。
上传多个文件
你也可以上传多个文件。这将会把 URL 保存到 JSON 中:
use Filament\Forms\Components\FileUpload;
FileUpload::make('attachments')
->multiple()
或者,你也可以传递布尔值来控制是否可以一次上传多个文件:
use Filament\Forms\Components\FileUpload;
FileUpload::make('attachments')
->multiple(FeatureFlag::active())