0
0
LaravelHow-ToBeginner · 4 min read

How to Use Notification in Laravel: Simple Guide

In Laravel, you use the Notification facade or the notify() method on user models to send notifications. First, create a notification class with php artisan make:notification, then send notifications via channels like mail, database, or broadcast.
📐

Syntax

Laravel notifications use classes that define how and where to send messages. You create a notification class, then send it to users or other notifiable entities.

  • Notification::send($users, new NotificationClass()): Sends notification to multiple users.
  • $user->notify(new NotificationClass()): Sends notification to a single user.
  • Notification classes define channels like mail, database, or broadcast.
php
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return ['mail', 'database'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Your invoice has been paid!')
                    ->action('View Invoice', url('/invoices'))
                    ->line('Thank you for using our application!');
    }

    public function toDatabase($notifiable)
    {
        return [
            'invoice_id' => 1234,
            'amount' => '$100',
        ];
    }
}
💻

Example

This example shows how to create a notification and send it to a user via email and database channels.

php
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class WelcomeUser extends Notification
{
    public function via($notifiable)
    {
        return ['mail', 'database'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->subject('Welcome to Our App')
                    ->line('Thank you for registering!')
                    ->action('Visit Dashboard', url('/dashboard'));
    }

    public function toDatabase($notifiable)
    {
        return [
            'message' => 'Welcome to our application!'
        ];
    }
}

// Sending notification in a controller or route
use App\Models\User;
use App\Notifications\WelcomeUser;

$user = User::find(1);
$user->notify(new WelcomeUser());
Output
An email is sent to the user and a notification record is saved in the database notifications table.
⚠️

Common Pitfalls

Common mistakes when using Laravel notifications include:

  • Not running php artisan notifications:table and migrating before using database notifications.
  • Forgetting to add Notifiable trait to the User model.
  • Not configuring mail settings properly, causing email notifications to fail.
  • Using notify() on models that do not implement the Notifiable trait.
php
/* Wrong: User model missing Notifiable trait */
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    // Missing use Notifiable;
}

/* Right: Add Notifiable trait */
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class User extends Model
{
    use Notifiable;
}
📊

Quick Reference

ActionCode ExampleDescription
Create Notificationphp artisan make:notification NotificationNameGenerates a new notification class.
Send to Single User$user->notify(new NotificationName())Sends notification to one user.
Send to Multiple UsersNotification::send($users, new NotificationName())Sends notification to many users.
Add Notifiable Traituse Notifiable; in User modelAllows model to receive notifications.
Database Notificationsphp artisan notifications:table && migrateCreates table to store notifications.

Key Takeaways

Use the Notifiable trait on models to enable notifications.
Create notification classes with artisan and define channels like mail or database.
Send notifications using $user->notify() or Notification::send() for multiple users.
Run migrations for database notifications before using them.
Check mail configuration to ensure email notifications work properly.