F
Filament9mo ago
Marc

How can I get the created model from createOptionForm?

I need to get the model that is created using createOptionForm(), I have tried using createOptionAction(), but I can't get the form. So I have had to rewrite the action() to make it work, but I know it is not correct and I need to find an alternative to do this, does anyone have an idea? I have tried to get the model from: $component, $model, $record, $state But none of these options work.
Select::make()->...->createOptionAction(static fn ($action) =>
$action->action(static function (Action $action, array $arguments, Select $component, array $data, ComponentContainer $form) {
if (!$component->getCreateOptionUsing()) {
throw new \Exception("Select field [{$component->getStatePath()}] must have a [createOptionUsing()] closure set.");
}

$createdOptionKey = $component->evaluate($component->getCreateOptionUsing(), [
'data' => $data,
'form' => $form,
]);

$state = $component->isMultiple()
? [
...$component->getState(),
$createdOptionKey,
]
: $createdOptionKey;

$component->state($state);
$component->callAfterStateUpdated();

$form->getRecord()->notifyCreate(password: $data['password']);

if (!($arguments['another'] ?? false)) {
return;
}

$action->callAfter();

$form->fill();

$action->halt();
}))
Select::make()->...->createOptionAction(static fn ($action) =>
$action->action(static function (Action $action, array $arguments, Select $component, array $data, ComponentContainer $form) {
if (!$component->getCreateOptionUsing()) {
throw new \Exception("Select field [{$component->getStatePath()}] must have a [createOptionUsing()] closure set.");
}

$createdOptionKey = $component->evaluate($component->getCreateOptionUsing(), [
'data' => $data,
'form' => $form,
]);

$state = $component->isMultiple()
? [
...$component->getState(),
$createdOptionKey,
]
: $createdOptionKey;

$component->state($state);
$component->callAfterStateUpdated();

$form->getRecord()->notifyCreate(password: $data['password']);

if (!($arguments['another'] ?? false)) {
return;
}

$action->callAfter();

$form->fill();

$action->halt();
}))
Another option that I have found is the following:
->createOptionAction(static function (Action $action, $state) {
$oldState = $state;
$action->after(static function ($state, $data) use ($oldState) {
$id = collect($state)->diff($oldState)->first();
Admin::find($id)->notifyCreate(password: $data['password']);
});
})
->createOptionAction(static function (Action $action, $state) {
$oldState = $state;
$action->after(static function ($state, $data) use ($oldState) {
$id = collect($state)->diff($oldState)->first();
Admin::find($id)->notifyCreate(password: $data['password']);
});
})
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server