Is there a reason why a DeleteAction wouldn't trigger a model observer event?

Using a DeleteAction in my class and it doesn't seem to be triggering the delete event on the model observer. Could it be because $this->item is just a generic Model class and not the specific model type I'm deleting (I'm using this class for a couple different types of models)? It's deleting it in the database correctly, it's just not firing the event in the observer as far as I can tell.
public function deleteAction()
{
return DeleteAction::make()
->record($this->item)
->requiresConfirmation()
->modalHeading('Delete Item')
->modalDescription($this->getModalDeleteMessage())
->hidden(auth()->user()->cannot('delete', $this->item))
->successNotification(
Notification::make()
->success()
->title('Item deleted'),
)
->after(function () {
$this->item->update(['deleted_by' => auth()->user()->id]);
$this->dispatch('refreshDocuments')->to(Documents::class);
});
}
public function deleteAction()
{
return DeleteAction::make()
->record($this->item)
->requiresConfirmation()
->modalHeading('Delete Item')
->modalDescription($this->getModalDeleteMessage())
->hidden(auth()->user()->cannot('delete', $this->item))
->successNotification(
Notification::make()
->success()
->title('Item deleted'),
)
->after(function () {
$this->item->update(['deleted_by' => auth()->user()->id]);
$this->dispatch('refreshDocuments')->to(Documents::class);
});
}
3 Replies
Jon Mason
Jon Mason4mo ago
That doesn't appear to be the problem. I modified my DeleteAction like below and still no model events are fired.
public function deleteAction()
{
return DeleteAction::make()
// ->record($this->item)
->requiresConfirmation()
->modalHeading('Delete Item')
->modalDescription($this->getModalDeleteMessage())
->hidden(auth()->user()->cannot('delete', $this->item))
->successNotification(
Notification::make()
->success()
->title('Item deleted'),
)
->action(function () {
if ($this->item instanceof Document) {
$document = Document::find($this->item->id);
$document->update(['deleted_by' => auth()->user()->id]);
$document->delete();
} else {
$this->item->delete();
}

$this->dispatch('refreshDocuments')->to(Documents::class);
});
}
public function deleteAction()
{
return DeleteAction::make()
// ->record($this->item)
->requiresConfirmation()
->modalHeading('Delete Item')
->modalDescription($this->getModalDeleteMessage())
->hidden(auth()->user()->cannot('delete', $this->item))
->successNotification(
Notification::make()
->success()
->title('Item deleted'),
)
->action(function () {
if ($this->item instanceof Document) {
$document = Document::find($this->item->id);
$document->update(['deleted_by' => auth()->user()->id]);
$document->delete();
} else {
$this->item->delete();
}

$this->dispatch('refreshDocuments')->to(Documents::class);
});
}
Xavi
Xavi3mo ago
@Jon Mason do you find a solution for trigger model obserservers? thanks!
awcodes
awcodes3mo ago
You shouldn’t have to trigger a model observer. Laravel triggers those.