F
Filament11mo ago
Andrius

Access resource record/model in custom component registered via registerRenderHook

What I am trying to do: I want to use custom livewire component (to use form builder) and register it with hook at position
panels::page.header-widgets.after
panels::page.header-widgets.after
to a certain resource scope. What I did: I've created livewire component, registered it. Everything works fine, component gets rendered in position I've provided.
FilamentView::registerRenderHook(
'panels::page.header-widgets.after',
fn () => Blade::render("@livewire('my-component')"),
scopes: App\Filament\Resources\ArticleResource\Pages\EditArticle::class
);
FilamentView::registerRenderHook(
'panels::page.header-widgets.after',
fn () => Blade::render("@livewire('my-component')"),
scopes: App\Filament\Resources\ArticleResource\Pages\EditArticle::class
);
My issue: Although everything renders fine, I want to use my resource(in which component is rendered) record. I can't find way to get my current
EditArticle
EditArticle
resource page record. Is it possible to get current record of resource (if it is edit page) in custom livewire component ?
2 Replies
Andrius
AndriusOP11mo ago
Only way I found to get current edit resource record ID (assuming that I hooked this component to certain Model) is like this:
$recordId = Route::getCurrentRoute()->parameter('record');
$record = Article::find($recordId);
$recordId = Route::getCurrentRoute()->parameter('record');
$record = Article::find($recordId);
Is there any other way to get record, or only this one ?
mark.cameron
mark.cameron8mo ago
@Andrius I was running into this issue today too, and I tried putting the registerRenderHook in a Middleware instead of a Service provider. I guess you could probably still do this through a service provider, but it wouldn't be as clean... Then in the Custom Middleware handle() method I run my checks for where to add the RenderHook (kind of like using the scopes: method parameter),
$model = Model::find($request->route('record'));

FilamentView::registerRenderHook(
PanelsRenderHook::PAGE_END,
fn (): string => Blade::render('@livewire(\'my-component\', [\'record\' => $model])', ['model' => $model]),
);
$model = Model::find($request->route('record'));

FilamentView::registerRenderHook(
PanelsRenderHook::PAGE_END,
fn (): string => Blade::render('@livewire(\'my-component\', [\'record\' => $model])', ['model' => $model]),
);
Then in my Livewire component I have a class property which gets populated with the parameter in the @livewire(...) string from the blade component
public Model $record;
public Model $record;
Want results from more Discord servers?
Add your server