How to Save Notifications in the Database in Laravel
- Get link
- X
- Other Apps
How to Save Notifications in the Database in Laravel
Laravel provides a built-in way to store notifications in the database using the database
channel. Here’s how you can set it up.
1. Create the Notifications Table
Laravel comes with a built-in migration for notifications. If you haven’t migrated it yet, run:
php artisan notifications:table php artisan migrate
This will create a notifications
table in your database with fields like:
id
(unique identifier)type
(notification class)notifiable_id
¬ifiable_type
(user or model that received the notification)data
(JSON field storing notification details)read_at
(timestamp for when the notification is read)
2. Update Your Notification Class
Modify your CommentAddedNotification
to use the database
channel and store data.
Example: app/Notifications/CommentAddedNotification.php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class CommentAddedNotification extends Notification implements ShouldQueue
{
use Queueable;
protected $task;
protected $comment;
public function __construct($task, $comment)
{
$this->task = $task;
$this->comment = $comment;
}
// Define which channels the notification should be sent through
public function via($notifiable): array
{
return ['database']; // Store the notification in the database
}
// Define the data that should be stored in the database
public function toArray($notifiable): array
{
return [
'task_id' => $this->task->id,
'task_title' => $this->task->title,
'comment' => $this->comment,
'commented_by' => auth()->user()->name,
'created_at' => now(),
];
}
}
3. Send the Notification
You can send the notification to a user like this:
$user->notify(new CommentAddedNotification($task, $comment));
Alternatively, you can use the Notification
facade:
use App\Notifications\CommentAddedNotification;use Illuminate\Support\Facades\Notification;
Notification::send($user, new CommentAddedNotification($task, $comment));
4. Retrieve Stored Notifications
In your controller or view, fetch the user’s notifications:
php$notifications = auth()->user()->notifications; // Get all notifications
If you only want unread notifications:
$unreadNotifications = auth()->user()->unreadNotifications;
5. Mark Notifications as Read
Once a user views the notification, mark it as read:
auth()->user()->unreadNotifications->markAsRead();
To mark a specific notification as read:
$notification = auth()->user()->notifications()->find($notificationId);
if ($notification) {
$notification->markAsRead();
}
6. Display Notifications in Blade
Example: Showing Notifications in a Dashboard
In resources/views/notifications.blade.php
:
<ul> @foreach(auth()->user()->notifications as $notification) <li> <strong>{{ $notification->data['task_title'] }}</strong> - {{ $notification->data['comment'] }} (by {{ $notification->data['commented_by'] }}) <small>{{ $notification->created_at->diffForHumans() }}</small> @if(!$notification->read_at) <form method="POST" action="{{ route('notifications.read', $notification->id) }}"> @csrf <button type="submit">Mark as Read</button> </form> @endif </li> @endforeach </ul>
7. Create a Route and Controller Method
In routes/web.php
:
use App\Http\Controllers\NotificationController;
Route::post('/notifications/read/{id}', [NotificationController::class, 'markAsRead'])->name('notifications.read');
In app/Http/Controllers/NotificationController.php
:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function markAsRead($id)
{
$notification = auth()->user()->notifications()->find($id);
if ($notification) {
$notification->markAsRead();
}
return back();
}
}
Summary
- Migrate the notifications table (
php artisan notifications:table && php artisan migrate
). - Update the notification class to use the
database
channel and store data. - Send notifications to users (
$user->notify(new CommentAddedNotification($task, $comment))
). - Retrieve notifications in a controller (
auth()->user()->notifications
). - Mark notifications as read using
markAsRead()
.
Now, notifications will be stored in the database and can be displayed in the UI! 🚀 Let me know if you need further customization.
- Get link
- X
- Other Apps
Comments
Post a Comment
What is your thought about this?