Alt*_*sin 1 php admin panel placeholder laravel-filament
我为 PostResource 制作了 CommentsRelationManager。
无论添加或删除评论,我如何刷新/重新计算标题上的计数值。
图片注释:我添加了新评论,但计数值没有刷新。
这是我的帖子资源表格:
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')->required(),
TextInput::make('body')->required(),
TextInput::make('count')
->reactive()
->label('count')
->disabled()
->placeholder(fn ($record) => $record->comments()->count())
]);
}
Run Code Online (Sandbox Code Playgroud)
这是我的 RelationManager
class CommentsRelationManager extends RelationManager
{ ...
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('comment'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}
}
Run Code Online (Sandbox Code Playgroud)
最后的建议是:
Emit a Livewire event from the relation manager after() the Edit or Create action,
and register a listener on the page, calling $refresh. This will refresh the value of the placeholder.
Run Code Online (Sandbox Code Playgroud)
但这对我来说并不清楚,我不知道该怎么做。
任何人都可以帮助我请...谢谢。
小智 6
根据您的最后建议,我通过此解决方案达到了预期的结果:
假设这些文件存在于 Filament/Resources 目录中
在EditPost.php文件中注册 livewire 事件监听器并使用 fillForm() 函数刷新表单
<?php
class EditPost extends EditRecord
{
....
protected $listeners = ['refresh'=>'refreshForm'];
....
public function refreshForm()
{
$this->fillForm();
}
}
Run Code Online (Sandbox Code Playgroud)
在 CommentsRelationManager.php 文件中使用 after() 函数来发出之前注册的 livewire 事件
<?php
....
use Filament\Resources\RelationManagers\RelationManager;
class CommentsRelationManager extends RelationManager
{
....
public static function table(Table $table): Table
{
return $table
->columns([
....
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make()
->after(function (RelationManager $livewire){
$livewire->emit('refresh');
})
,
])
}
}
Run Code Online (Sandbox Code Playgroud)