自定义组件
将 Blade 视图插入到 Schema
你可以使用视图组件,将 Blade 视图插入到 Schema 中:
use Filament\Schemas\Components\View;
View::make('filament.schemas.components.chart')
此处假定你有一个 resources/views/filament/schemas/components/chart.blade.php 文件。
渲染组件子 Schema
你可以传递 Schema 子组件到该组件的 schema() 方法中:
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\View;
View::make('filament.schemas.components.chart')
->schema([
TextInput::make('subtotal'),
TextInput::make('total'),
])
在 Blade 视图中,你可以使用 $getChildSchema() 函数渲染该组件的 schema():
<div>
{{ $getChildSchema() }}
</div>
在 Blade 视图中访问另一个组件的状态
在 Blade 视图中,你可以使用 $get() 函数访问 Schema 中另一个组件的状态:
<div>
{{ $get('email') }}
</div>
TIP
除非表单字段是响应式的,否则当字段值发生变化时,Blade 视图不会刷新,只有在下一次用户交互(即向服务器发出请求)时才会刷新。如果需要对字段值的变化做出响应,则应该使用 live()。
在 Blade 视图中访问 Eloquent 记录
在 Blade 视图中,你可以使用 $record 变量访问当前 Eloquent 记录:
<div>
{{ $record->name }}
</div>
在 Blade 视图中访问当前操作
在 Blade 视图中,你可以使用 $operation 变量访问当前操作,这些操作通常为 create、edit 或者 view:
<p>
@if ($operation === 'create')
This is a new post.
@else
This is an existing post.
@endif
</p>
在 Blade 视图中访问当前 Livewire 组件实例
在 Blade 视图中,你可以使用 $this 访问当前 Livewire 组件:
@php
use Filament\Resources\Users\RelationManagers\PostsRelationManager;
@endphp
<p>
@if ($this instanceof PostsRelationManager)
You are editing posts the of a user.
@endif
</p>
在 Blade 视图中访问当前组件实例
在 Blade 视图中,你可以使用 $schemaComponent 访问当前组件实例。你可以在该对象上调用公共方法访问其他以变量形式无法获取到的信息:
<p>
@if ($schemaComponent->getState())
This is a new post.
@endif
</p>
插入 Livewire 组件到 Schema 中
你可以直接将 Livewire 组件插入到 Schema 中:
use App\Livewire\Chart;
use Filament\Schemas\Components\Livewire;
Livewire::make(Chart::class)
NOTE
将 Livewire 组件插入到 Schema 中时,有些功能会受到限制。由于嵌套的 Livewire 组件是单独渲染的,因此只能访问可序列化的数据。因此,你无法渲染子 Schema、访问另一个组件的实时状态、访问当前的 Livewire 组件实例 或者 访问当前组件实例。只能访问传递给 Livewire 组件的静态数据 和 当前记录。由于这些限制,你应该渲染嵌套的 Livewire 组件而不是 Blade 视图 的情况很少见。
如果你要渲染多个相同的 Livewire 组件,请确保向每个组件传递唯一的 key():
use App\Livewire\Chart;
use Filament\Schemas\Components\Livewire;
Livewire::make(Chart::class)
->key('chart-first')
Livewire::make(Chart::class)
->key('chart-second')
Livewire::make(Chart::class)
->key('chart-third')
传递参数到 Livewire 组件中
你可以传递参数数组到 Livewire 组件中:
use App\Livewire\Chart;
use Filament\Schemas\Components\Livewire;
Livewire::make(Chart::class, ['bar' => 'baz'])
除了允许静态值职位,make() 方法也可以接受函数来动态计算其值。你可以将各种 utility 作为参数注入到函数中。
Learn more about utility injection.| Utility | Type | Parameter | Description |
|---|---|---|---|
| Component | Filament\Schemas\Components\Component | $component | The current component instance. |
| Get function | Filament\Schemas\Components\Utilities\Get | $get | A function for retrieving values from the current schema data. Validation is not run on form fields. |
| 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>. |
| Eloquent record | ?Illuminate\Database\Eloquent\Model | $record | The Eloquent record for the current schema. |
现在,这些参数将传给 Livewire 组件的 mount() 方法:
class Chart extends Component
{
public function mount(string $bar): void
{
// ...
}
}
此外,它们将作为 Livewire 组件上的公共属性提供:
class Chart extends Component
{
public string $bar;
}