How can I throw an exception and send a notification in handleRecordCreation() method

I am trying to send an email when I create a record If the email was not send an exception is thrown I am catching the exception then I am sending a notification But I am getting this error: App\Filament\Resources\TicketResource\Pages\CreateTicket::handleRecordCreation(): Return value must be of type Illuminate\Database\Eloquent\Model, none returned
protected function handleRecordCreation(array $data): Model
{
try {
//..
Mail::to($created->customer)->send(new TicketCreated($title));
return $created;
} catch (Exception $e) {
if (App::environment('local')) {
Notification::make()
->title($e->getMessage())
->danger()
->send();
} else {
Notification::make()
->title('Something went wrong')
->danger()
->send();
}
}
}
protected function handleRecordCreation(array $data): Model
{
try {
//..
Mail::to($created->customer)->send(new TicketCreated($title));
return $created;
} catch (Exception $e) {
if (App::environment('local')) {
Notification::make()
->title($e->getMessage())
->danger()
->send();
} else {
Notification::make()
->title('Something went wrong')
->danger()
->send();
}
}
}
How can I handle this?
6 Replies
Josh777
Josh77713mo ago
You are not returning an instance of the Model outside of the try
LancelotGamer
LancelotGamerOP13mo ago
Yes I know that I want to send a notification that there was an error before creating the record
Josh777
Josh77713mo ago
Then you will probably want to try this in an Observer class instead, using creating / create methods
LancelotGamer
LancelotGamerOP13mo ago
protected function beforeCreate(): void
{
// Runs before the form fields are saved to the database.
}
protected function beforeCreate(): void
{
// Runs before the form fields are saved to the database.
}
I don't want to use Observers / beforeCreate() method because I have DB::transaction()
Josh777
Josh77713mo ago
Do you have more context?
Want results from more Discord servers?
Add your server