custom update

Hello, inside the handleRecordUpdate method of an Editrecord I have an update method for my repository that returns whether the update was successful or not. Then I would like to know how I do it so that when my response from the repository gives an error, it shows a notification and remains on the Update page and when it works, it goes to the listing page with a success notification. Thanks
class EditRole extends EditRecord
{
use LivewireAlert;

protected static string $resource = RoleResource::class;

protected function getActions(): array
{
return [
//
];
}

protected function handleRecordUpdate(Model $record, array $data): Model
{
$result = app(RoleRepository::class)->update($data, $record->id);
if ($result['erro']) {
$this->alert('error', $result['mensagem'], [
'position' => 'center',
'timer' => '',
'toast' => false,
'showConfirmButton' => true,
'onConfirmed' => '',
'confirmButtonText' => 'Ok',
'width' => '660',
]);
return $record;
} else {
Notification::make()
->title($result['mensagem'])
->success()
->send();
}

return $result['model'];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
class EditRole extends EditRecord
{
use LivewireAlert;

protected static string $resource = RoleResource::class;

protected function getActions(): array
{
return [
//
];
}

protected function handleRecordUpdate(Model $record, array $data): Model
{
$result = app(RoleRepository::class)->update($data, $record->id);
if ($result['erro']) {
$this->alert('error', $result['mensagem'], [
'position' => 'center',
'timer' => '',
'toast' => false,
'showConfirmButton' => true,
'onConfirmed' => '',
'confirmButtonText' => 'Ok',
'width' => '660',
]);
return $record;
} else {
Notification::make()
->title($result['mensagem'])
->success()
->send();
}

return $result['model'];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
2 Replies
Patrick Boivin
I think you're looking for the ->halt() method, have a look in this section of the docs : https://filamentphp.com/docs/2.x/admin/resources/editing-records#halting-the-saving-process
joao nivaldo
joao nivaldoOP2y ago
Thank you it worked out great.
Want results from more Discord servers?
Add your server