Logics Guru

Send Database and Email Notifications in Laravel

Send Laravel 13 notifications through mail and database channels and test delivery without sending messages.

2 min read Intermediate
Technical illustration for Send Database and Email Notifications in Laravel

Send Laravel 13 notifications through mail and database channels and test delivery without sending messages. This tutorial focuses on a small implementation you can run, inspect and extend instead of hiding the important behavior behind scaffolding.

Prerequisites#

  • Laravel 13, PHP, Composer and a configured test database
  • Use placeholder credentials and a non-production environment.
  • Know the basic syntax of Laravel.

What we will build#

We will implement the core path, exercise it with a realistic request or test, and identify the production concerns that should remain outside a minimal example.

Step 1: Prepare the project#

Create a focused branch and confirm the current application or sample database works before changing it. Keep secrets in environment variables, commit an example environment file without values, and make the smallest schema change that supports the use case.

Step 2: Implement the core behavior#

PHP
class InvoicePaid extends Notification implements ShouldQueue
{
    use Queueable;

    public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

    public function toArray(object $notifiable): array
    {
        return ['invoice_id' => $this->invoice->id];
    }
}

$user->notify(new InvoicePaid($invoice));

Keep validation at the boundary. Allow-list client-controlled fields, return an explicit response, and avoid exposing internal exceptions or credentials.

Step 3: Test the successful path#

Shell
Notification::fake();
$user->notify(new InvoicePaid($invoice));
Notification::assertSentTo($user, InvoicePaid::class);

Confirm both the response and the resulting state. A status code alone is not enough when the operation changes a database, dispatches work or writes a file.

Step 4: Test failures#

Repeat the test with missing fields, invalid types, unauthorized access and a dependency failure. The application should reject the request predictably without leaking a stack trace, secret or filesystem path.

Common problems#

  • Inputs are trusted too early: validate and normalize at the system boundary.
  • The example works only once: test repeated and concurrent requests where the operation changes state.
  • Errors are inconsistent: use one documented error shape and attach a request identifier in production.

Best practices#

  • Use the current supported runtime and pin important dependencies.
  • Keep examples minimal, but preserve authentication, validation and error handling.
  • Add automated coverage for the behavior most likely to regress.
  • Log identifiers and outcomes, never passwords, tokens or complete sensitive payloads.

Conclusion#

You now have a working foundation for send database and email notifications in laravel. Extend it by separating infrastructure from business rules, adding authorization where resources belong to users, and measuring the behavior under realistic data and failure conditions.

Mustasim Ali

Mustasim Ali

Senior Software Engineer & Technical Lead

Full-stack engineer working in PHP and Laravel since 2019. I lead a development team building web and mobile products, and spend most of my time in Laravel, Node.js, Vue and React against MySQL and MongoDB. Logics Guru is where I write up the things I had to work out the hard way — the architecture decisions, the debugging sessions, and the small utilities I kept rebuilding until I put them somewhere permanent. Everything here is what I actually use.

Related

PHP

Create a Secure Login System with PHP

Create session-based login in PHP 8.4 with password hashing, prepared statements and session regeneration...

Mustasim Ali 2 min Intermediate