F
Filament3mo ago
o.m

Is it possible to retrieve the parent id of the relationship?

I have a repeater for questions and its like this, Questions -> options (list of questions)
Answers::make('options')->addAction(
fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Set $set, array $state) {
$uuid = Str::uuid()->toString();

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' = > true, // disable newly added item to options
'ordinal' = > count($state) + 1,
];

$set('options', $state);
}))
Answers::make('options')->addAction(
fn(Forms\Components\Actions\Action $action) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Set $set, array $state) {
$uuid = Str::uuid()->toString();

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' = > true, // disable newly added item to options
'ordinal' = > count($state) + 1,
];

$set('options', $state);
}))
I wanted to access it's parent's id so I could add it to the state[]
15 Replies
toeknee
toeknee3mo ago
Try this, $livewire and $ownerRecord.
Answers::make('options')->addAction(
fn(Forms\Components\Actions\Action $action, $livewire) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Set $set, array $state) {
$uuid = Str::uuid()->toString();

// Get Parent/ ownerRecord
$parentId = $livewire->ownerRecord->id;

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' = > true, // disable newly added item to options
'ordinal' = > count($state) + 1,
];

$set('options', $state);
}))
Answers::make('options')->addAction(
fn(Forms\Components\Actions\Action $action, $livewire) =
> $action->link()
->label('Add an Answer')
->icon('heroicon-o-plus')
->action(function(Forms\Set $set, array $state) {
$uuid = Str::uuid()->toString();

// Get Parent/ ownerRecord
$parentId = $livewire->ownerRecord->id;

$state[] = [
'uuid' = > $uuid,
'label' = > '',
'is_correct' = > false,
'disabled' = > true, // disable newly added item to options
'ordinal' = > count($state) + 1,
];

$set('options', $state);
}))
o.m
o.mOP3mo ago
Got this error unfortunately
No description
o.m
o.mOP3mo ago
But I was Able to solve it using $get('id')
toeknee
toeknee3mo ago
ahh your trying to do the action on the resource, that your dong there is spot on with get. What I provided works on say relationship managers when editing etc
o.m
o.mOP3mo ago
I see, how'd did you discover $livewire parameter?
toeknee
toeknee3mo ago
It's well known within Filament.
o.m
o.mOP3mo ago
I barely saw it in the docs
o.m
o.mOP3mo ago
Ah specfic to relation manager
toeknee
toeknee3mo ago
No, it is part of the components generally, but it's not something you want to use much if you can avoid as it's the whole component object being called again.
o.m
o.mOP3mo ago
Is there an option to refresh the page iside the action?
toeknee
toeknee3mo ago
Page or the form? $form->fill($newData) ? or $form->refresh()?
o.m
o.mOP3mo ago
Form But inside the addAction() is that possible?
toeknee
toeknee3mo ago
Hmmm You could put a watcher on it, and then refresh it after emiting the event?
o.m
o.mOP3mo ago
Possible. Was thingking of just a function call or something like that but didn't work though

Did you find this page helpful?