How to Create Laravel Database Notifications
- Introduction
- What Are Laravel Database Notifications?
- Step 1: Add Notifiable Trait to the User Model
- Step 2: Create the Notification Table
- Step 3: Create a Notification Class
- Step 4: Define the toDatabase() Method
- Step 5: Send the Notification
- Step 6: Retrieve and Display Notifications
- Step 7: Mark Notifications as Read
- Step 8: Make Notifications Queueable (Optional)
- Conclusion
Introduction
If you're looking to implement Laravel database notification in your Laravel application, you're in the right place. This tutorial walks you through the process of creating and sending database notifications using Laravel's built-in notification system.
What Are Laravel Database Notifications?
Laravel provides a unified API for sending notifications via multiple channels, including mail, SMS, Slack, and database. Database notifications are stored in your application's database and can be retrieved and displayed to users at any time.
Step 1: Add Notifiable Trait to the User Model
To use notifications, your User model must use the Notifiable trait:
use Illuminate\Notifications\Notifiable; class User extends Authenticatable{ use Notifiable;}
This trait gives the user model access to helper methods for sending and managing notifications.
Step 2: Create the Notification Table
First, publish the notifications migration:
php artisan notifications:table
Then run the migration:
php artisan migrate
This creates a notifications table in your database that will store all your notifications.
Step 3: Create a Notification Class
Run the Artisan command to generate a notification:
php artisan make:notification NewUserRegistered
This will create a file in app/Notifications/NewUserRegistered.php.
Step 4: Define the toDatabase() Method
Open NewUserRegistered.php and define how the notification should be stored in the database using the toDatabase() method:
namespace App\Notifications; use Illuminate\Notifications\Notification;use App\Models\User; class NewUserRegistered extends Notification{ protected $newUser; public function __construct(User $newUser) { $this->newUser = $newUser; } public function via(object $notifiable): array { return ['database']; } public function toDatabase(object $notifiable): array { return [ 'id' => $this->newUser->id, 'name' => $this->newUser->name, 'email' => $this->newUser->email, ]; }}
Step 5: Send the Notification
You can send the notification to any notifiable entity (typically a User model):
use App\Models\User;use App\Notifications\NewUserRegistered; $user = User::find(1); // Admin user$newUser = User::find(2); // Newly registered user $user->notify(new NewUserRegistered($newUser));
Alternatively, you can use the Notification facade:
use Illuminate\Support\Facades\Notification; Notification::send($user, new NewUserRegistered($newUser));
Step 6: Retrieve and Display Notifications
You can retrieve a user's notifications using the notifications relationship:
$notifications = auth()->user()->notifications;
To display them:
@foreach(auth()->user()->notifications as $notification) <div class="notification"> New user registered: {{ $notification->data['name'] }} ({{ $notification->data['email'] }}) </div>@endforeach
Step 7: Mark Notifications as Read
You can mark notifications as read using:
auth()->user()->unreadNotifications->markAsRead();
Or mark a single notification:
$notification = auth()->user()->notifications()->first();$notification->markAsRead();
Step 8: Make Notifications Queueable (Optional)
To queue a notification, implement the ShouldQueue interface and use the Queueable trait:
namespace App\Notifications; use Illuminate\Bus\Queueable;use Illuminate\Notifications\Notification;use Illuminate\Contracts\Queue\ShouldQueue;use App\Models\User; class NewUserRegistered extends Notification implements ShouldQueue{ use Queueable; protected $newUser; public function __construct(User $newUser) { $this->newUser = $newUser; } public function via(object $notifiable): array { return ['database']; } public function toDatabase(object $notifiable): array { return [ 'id' => $this->newUser->id, 'name' => $this->newUser->name, 'email' => $this->newUser->email, ]; }}
Tip: Make sure your queue worker is running with
php artisan queue:workto process queued notifications.
Conclusion
Creating a Laravel database notification system is clean and straightforward. Notifications are a powerful way to improve user engagement, and Laravel makes it incredibly simple to implement.
Feel free to extend this with broadcasting, frontend updates, or user preferences for notification types.
Stay Updated.
I'll you email you as soon as new, fresh content is published.