How to Send Notification via Database in Laravel
In Laravel, you send notifications via the database by creating a notification class with the
database channel and storing notification data in the database. Use php artisan notifications:table to create the notifications table, then call notify() on the user model to send the notification.Syntax
To send a notification via the database in Laravel, you create a notification class that uses the database channel. The notification data is stored as JSON in the notifications table.
Key parts:
via(): Defines the channels, include'database'.toDatabase(): Returns the array of data saved in the database.notify(): Called on the user or notifiable model to send the notification.
php
php artisan notifications:table php artisan migrate // Notification class example use Illuminate\Notifications\Notification; class InvoicePaid extends Notification { public function via($notifiable) { return ['database']; } public function toDatabase($notifiable) { return [ 'invoice_id' => 1234, 'amount' => 2500, 'message' => 'Your invoice has been paid.', ]; } } // Sending notification $user->notify(new InvoicePaid());
Example
This example shows how to create a notification that stores data in the database and how to send it to a user.
php
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; class InvoicePaid extends Notification { public function via($notifiable) { return ['database']; } public function toDatabase($notifiable) { return [ 'invoice_id' => 5678, 'amount' => 1500, 'message' => 'Your invoice #5678 has been paid successfully.', ]; } } // In a controller or route use App\Models\User; use App\Notifications\InvoicePaid; $user = User::find(1); $user->notify(new InvoicePaid()); // To retrieve notifications $notifications = $user->notifications; foreach ($notifications as $notification) { echo $notification->data['message'] . "\n"; }
Output
Your invoice #5678 has been paid successfully.
Common Pitfalls
- Not running
php artisan notifications:tableand migrating, so the notifications table does not exist. - Forgetting to include
'database'in thevia()method. - Not returning an array in
toDatabase()method; it must be an array to store as JSON. - Trying to notify a model that does not use the
Notifiabletrait.
php
// Wrong: Missing 'database' channel public function via($notifiable) { return ['mail']; // database channel missing } // Right: Include 'database' public function via($notifiable) { return ['database']; } // Wrong: Returning string instead of array public function toDatabase($notifiable) { return 'Invoice paid'; // should be array } // Right: Return array public function toDatabase($notifiable) { return ['message' => 'Invoice paid']; }
Quick Reference
Steps to send database notifications in Laravel:
- Run
php artisan notifications:tableandphp artisan migrateto create the notifications table. - Create a notification class with
php artisan make:notification. - In the notification, use
via()to include'database'channel. - Define
toDatabase()to return the data array. - Call
notify()on the user or notifiable model.
Key Takeaways
Always run the notifications migration to create the database table before sending notifications.
Include 'database' in the via() method to store notifications in the database.
Return an array from toDatabase() with the data you want to save.
Use the Notifiable trait on models to enable notifications.
Retrieve notifications from the user model using the notifications relationship.