Pass arguments to `DeleteAction` in Livewire component
Is it possible to add a
DeleteAction
to a component instead of a simple Action
and pass arguments to it without overriding the action
method? (https://filamentphp.com/docs/3.x/actions/adding-an-action-to-a-livewire-component#passing-action-arguments)
Here is some sample code (that doesn't work)
I can do something like
But that kind of defeats the purpose of using a DeleteAction
in the 1st place.
I tried a couple of things like
but nothing seems to work.
Any tips/ideas? Am I missing something?12 Replies
My last attempt is this abomination
But this also doesn't work. I get
Too few arguments to function App\Livewire\MyComponent::deleteMessage(), 0 passed
This doesn't work either unfortunately
hello @ChesterS
It seems like you are trying to add a DeleteAction to a component in Filament and pass arguments to it without overriding the action method.
right?
Yes
Based on the code you provided, it appears that you're trying to call the
deleteMessage()
method and pass the $message->id
as an argument within a loop.Sorry that was just an example to demonstrate what I'm trying to achieve.
here is a more
sane
code
Unfortunately, this approach won't work as you cannot directly pass arguments to a DeleteAction without overring the action method.
yeah
One possible slution is to modify your code as folows
php
// MyComponent.php
public function deleteMessage($messageId): Action
{
$message = Message::find($messageId);
return DeleteAction::make('deleteMessage')
->record($message);
}
html
<!-- Template -->
@foreach($this->messages as $message)
<button wire:click="deleteMessage({{ $message->id }})">Delete</button>
@endforeach
I hope this helps!Hmm this doesn't seem to work š¤ Also you lose a lot of the features of using an action (styling, notifications, animations etc) since you create your own button.
just second
public $selectedMessageId;
Define a public property in your component to store the selected message ID
and
public function deleteMessage($messageId): Action
{
$this->selectedMessageId = $messageId;
return DeleteAction::make('deleteMessage')
->confirm('Are you sure you want to delete this message?')
->onSuccess(function () {
// Perform deletion logic here
$message = Message::find($this->selectedMessageId);
$message->delete();
// Show success notification or perform any other actions
$this->alert('success', 'Message deleted successfully!');
});
}
šHey thanks for the message. Yeah this might work but it negates the point of using actions. Not only does it require extra properties for each Component I might need it, but it is also a hacky approach where I duplicate the delete logic on the
onSuccess
call.
All I really need is access to the $arguments
variable in the record()
method since that is my real problem - I can't set a record on the Action
Also, 'DeleteMessage' method needs to be called 'DeleteMessageAction'
@awcodes How would I set the record on the
but it doesn't work as $arguments is empty
DeleteAction
? eg
gives me the following error
Filament\Actions\DeleteAction::Filament\Actions\{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given, called in /var/www/endor/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35
I tried setting the record
usingbut it doesn't work as $arguments is empty
look at the DeleteAction to see how they are doing it under the hood
This is also what it's better to use custom Actions than trying to override one of the prebuild actions.