Send a simple email after a resource is created
Hi all, I am new to the laravel and filament community and I have managed to create a new notification php artisan make:notification TicketCreatedNotification
How do I send this notification after a new Ticket resource is created.
Looking online they refer to Filament methods afterCreate() and afterSave(), but when I try and implement these suggestions I get an error that the methods do not exist,
For example:
public static function form(Form $form): Form
{
return $form
->schema([
// ... your fields
])
->afterCreate(function ($record, $data) {
$record->notify(new TicketCreatedNotification($record));
});
}
Solution:Jump to solution
Could you link your notification to a Laravel Model Event? For example, whenever a model is created, fire off the Notification?
https://laravel.com/docs/10.x/eloquent#events...
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
12 Replies
protected function getCreatedNotificationTitle(): ?string
{
return 'Ticket created!';
}
In your create ticket file
This would only return a notification in the app, where I would like to send an email that the record was created. Or am I not understanding where you are coming from.
you want to send email to different system or what?
Yes to the user that created the ticket.
you can check send notificatioon in docs
not sure if that can help you
there is send email action as well
Solution
Could you link your notification to a Laravel Model Event? For example, whenever a model is created, fire off the Notification?
https://laravel.com/docs/10.x/eloquent#events
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
you can read through that docs and see what you can do
Thank you, I had a look at the documentation but from what I gather it looks like these actions are manual actions and does not take place automatically as soon as the resource is created.
Thank you Alex, I think you are on the right path. I will look into this. I was hoping Filament had an automatic way to trigger the email notification once the resource was created.
Best of luck!
Filament is still Laravel underneath, so all of the Laravel fundamentals still apply.
Personally, I'd say that firing off a notification on a model event isn't Filament's domain--it's entirely Laravel's because Laravel provides the event hooks natively. I'm open for other opinions on that though 😆
Agreed, I just need to recall how I went about doing this. It appears that I need to have a look at the booted method of my Model. And then target the created event.
Yep! The
booted()
method works perfectly fine.
I tend to prefer creating my own event classes and mapping the model events to them once the logic starts getting more complex, but the booted()
method is a great option for simple events!Thanks again for your assistance.