Delete a Record intentionally with relationships

Hey everyone. So, I want to manually take care of a record deletion. There's a few relationships involved, and I want to use DB::transactions. I know I could use an Observer, but that's not explicit and I believe it won't use transactions. My only issue is that after deleting the record, it doesn't navigate away. It seems it tries to reload the page before redirecting, and of course, the model is already gone by that time, and I get a 404, instead of a redirect. Here is the code I'm using on a custom action (tried to customize a DeleteAction before, but similar result. Thanks for any tips!
Actions\Action::make('delete_with_sections')->requiresConfirmation()
// not working
->successRedirectUrl(PageResource::getUrl())
->action(function (Action $action) {

DeletePage::handle($this->getRecord());

// not working
$this->redirect(PageResource::getUrl());
}),
Actions\Action::make('delete_with_sections')->requiresConfirmation()
// not working
->successRedirectUrl(PageResource::getUrl())
->action(function (Action $action) {

DeletePage::handle($this->getRecord());

// not working
$this->redirect(PageResource::getUrl());
}),
6 Replies
jigsaw
jigsaw3mo ago
Is this page index or edit?
Patricio
Patricio3mo ago
edit
LeandroFerreira
LeandroFerreira3mo ago
hey! What about a custom delete action?
class CustomDeleteAction extends DeleteAction
{
protected function setUp(): void
{
parent::setUp();

$this->action(function (): void {
$result = $this->process(static function (Model $record) {

try {

DB::beginTransaction();

//...

DB::commit();

return $record->delete();

} catch (\Exception $e) {
DB::rollBack();
}

});

if (! $result) {

Notification::make('error')
->danger()
->title('Error')
->send();

return;
}

$this->success();
});
}
}
class CustomDeleteAction extends DeleteAction
{
protected function setUp(): void
{
parent::setUp();

$this->action(function (): void {
$result = $this->process(static function (Model $record) {

try {

DB::beginTransaction();

//...

DB::commit();

return $record->delete();

} catch (\Exception $e) {
DB::rollBack();
}

});

if (! $result) {

Notification::make('error')
->danger()
->title('Error')
->send();

return;
}

$this->success();
});
}
}
Patricio
Patricio3mo ago
Hey Leandro! I tried it, not working
LeandroFerreira
LeandroFerreira3mo ago
hum.. are you able to create a mini repo on Github to reproduce this issue?
Patricio
Patricio3mo ago
Sure, I'll send you privately the current one
Want results from more Discord servers?
Add your server