Skip to content

Conversation

@gdebrauwer
Copy link
Contributor

When a notification is sent, I sometimes want to do something small, for example, update a single property on the eloquent model used by the notification. Currently, you have to create a dedicated listener that listens for the NotificationSent event.

class BookingNotification extends Notification
{
    public function __construct(
		public Booking $booking;
	) {
	}

	public function via()
	{
		return ['mail'];
	}

	public function toMail()
	{
		// ...
	}
}

class BookingNotificationListener
{
	public function handle(NotificationSent $event)
	{
		if ($event->notification instanceof BookingNotification) {
			$event->notification->booking->update(['notified_at' => now()]);
		}
	}
}

That is a lot of code to do something very simple.
This PR improves that by allowing you to implement an afterSending method in your notification class. Now you don't need a dedicated listener class anymore. When you look at the notification class, you also now immediately see what happens to the model after sending the notification instead of having to look for a separate class that contains some logic linked to that specific notification.

class BookingNotification extends Notification
{
    public function __construct(
		public Booking $booking;
	) {
	}

	public function via()
	{
		return ['mail'];
	}

	public function toMail()
	{
		// ...
	}

	public function afterSending($notifiable, $channel, $response)
	{
		$this->booking->update(['notified_at' => now()]);
	}
}

@taylorotwell taylorotwell merged commit e65df25 into laravel:12.x Feb 6, 2026
72 checks passed
@bretto36
Copy link
Contributor

@gdebrauwer Is this possible with the Mailable class too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants