F
Filamentβ€’3mo ago
Sylfel

CreateAction with arguments

Hi, i try to use CreateAction with arguments in my own Livewire component. Here a extract of my code : blade template :
@foreach ($categories as $category)
[...]
{{ ($this->addToCategoryAction)(['category' => $category->id]) }}
@endforeach
@foreach ($categories as $category)
[...]
{{ ($this->addToCategoryAction)(['category' => $category->id]) }}
@endforeach
component :
class Dashboard extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public function addToCategoryAction(): Action
{
return CreateAction::make('addToCategory')
->model(Note::class)
->form([
TextInput::make('label')
->required(function (): bool {
// here i would like to use argument "category"
})
->maxLength(255),
])
->mutateFormDataUsing(function (array $data, array $arguments): array {
// here i have access to arguments !
return $data;
});
}

}
class Dashboard extends Component implements HasActions, HasForms
{
use InteractsWithActions;
use InteractsWithForms;

public function addToCategoryAction(): Action
{
return CreateAction::make('addToCategory')
->model(Note::class)
->form([
TextInput::make('label')
->required(function (): bool {
// here i would like to use argument "category"
})
->maxLength(255),
])
->mutateFormDataUsing(function (array $data, array $arguments): array {
// here i have access to arguments !
return $data;
});
}

}
How can i use argument in required (or Select or ...) in formmethod ? Is it possible to loadCategory Model once, and use it in different sub-method in make method ? Thanks a lot !
Solution:
OOOOOhhhh !!! ```php ->form(function (array $arguments) { return [...]...
Jump to solution
8 Replies
mvenghaus
mvenghausβ€’3mo ago
you can define your own livewire property or build a service with cache or just define it before
Sylfel
Sylfelβ€’3mo ago
To Loading model, yes, but how to use it in required method ? fow now, i use this:
$this->getMountedAction()->getArguments()['category']
$this->getMountedAction()->getArguments()['category']
mvenghaus
mvenghausβ€’3mo ago
every callback is evaluated and resolves your params
Sylfel
Sylfelβ€’3mo ago
But it's a like a hack (IMO) every ... no ! Not this one
mvenghaus
mvenghausβ€’3mo ago
i do it that way
Solution
Sylfel
Sylfelβ€’3mo ago
OOOOOhhhh !!!
->form(function (array $arguments) {
return [...]
->form(function (array $arguments) {
return [...]
form can have a function ... !!!! THX a lot 😁
mvenghaus
mvenghausβ€’3mo ago
πŸ₯³
mvenghaus
mvenghausβ€’3mo ago
tell me please if it works when using a confirm modal .. would make my code a lot of easier πŸ™‚