Source: juststeveking.uk

Diving into Notifications
In most applications, we need to send notifications, whether in-app, email or slack - these are typically transactional notifications to alert users of some action or event in your application. The first thing you need to think about when wanting to send notifications to a user is the delivery mechanism.

Let’s start building our notification: declare(strict_types=1); namespace App\Notifications; use Carbon\CarbonInterface; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Notification; final class UserMeetingBookedNotification extends Notification implements ShouldQueue { use Queueable; public function __construct( public readonly string $name, public readonly string $email, public readonly CarbonInterface $datetime, ) {}}

We will use this notification to email the user to let them know they have a meeting booked.

it('can send an email notification when a booking is made', function () { Notification::fake(); $user = User::factory()->create(); expect( Bookings::query()->count(), )->toEqual(0); $action = app()->make( abstract: CreateNewBookingAction::class, ); $action->handle( user: $user->id, name: 'Test User', email: 'test@email.com', time: now()->addHours(12), ); expect( Bookings::query()->count(), )->toEqual(1); Notification::assertSentTo([$user], UserMeetingBookedNotification::class, ); }); We are faking the notification driver in Laravel so that the notification isn’t routed to anyone.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners