Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Cruorzy's avatar
Level 14

Notification bell feature

I feel like I have a certain need for a feature that lots of other already created.

I need a notification bell that keeps track of what notification a user has recieved and marked as read. (Like laracasts when someone has reacted to your post.)

I have found https://github.com/mikebarlow/megaphone which seems sort of what I need. But am curious if other packages exist or people just code it on their own.

0 likes
3 replies
LaryAI's avatar
Level 58

To implement a notification bell feature that tracks notifications and marks them as read, you can use Laravel's built-in notification system. Here's a step-by-step guide to help you set it up:

Step 1: Create a Notification

First, create a notification class using Artisan:

php artisan make:notification UserNotification

This will generate a new notification class in the app/Notifications directory.

Step 2: Define the Notification

Open the newly created UserNotification class and define the notification's delivery channels and content. For example:

Step 3: Trigger the Notification

You can trigger the notification from anywhere in your application. For example, when a user performs a specific action:

use App\Models\User;
use App\Notifications\UserNotification;

// Assuming $user is the user you want to notify
$user->notify(new UserNotification('You have a new notification!'));

Step 4: Display Notifications

To display notifications in your view, you can retrieve them from the database:

$notifications = auth()->user()->unreadNotifications;

Then, loop through them in your Blade template:

<ul>
    @foreach ($notifications as $notification)
        <li>
            {{ $notification->data['message'] }}
            <form method="POST" action="{{ route('notifications.read', $notification->id) }}">
                @csrf
                <button type="submit">Mark as Read</button>
            </form>
        </li>
    @endforeach
</ul>

Step 5: Mark Notifications as Read

Create a route and controller method to mark notifications as read:

// web.php
Route::post('/notifications/{id}/read', [NotificationController::class, 'markAsRead'])->name('notifications.read');

// NotificationController.php
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function markAsRead($id)
    {
        $notification = auth()->user()->notifications()->find($id);
        if ($notification) {
            $notification->markAsRead();
        }

        return redirect()->back();
    }
}

Step 6: Notification Bell Icon

You can add a notification bell icon in your layout and show the count of unread notifications:

<a href="#">
    <i class="fa fa-bell"></i>
    <span class="badge">{{ auth()->user()->unreadNotifications->count() }}</span>
</a>

This setup uses Laravel's built-in notification system to store notifications in the database and allows users to mark them as read. You can further customize the notification content and delivery channels as needed.

Cruorzy's avatar
Level 14

@martinbean Ah man, only if I looked through the docs first. Did some googling but never pulled up the docs, was so sure it must have been a package.

1 like

Please or to participate in this conversation.