A simple Laravel notification system for authenticated users.
Copy all the content of the laravel
folder into your laravel project.
Set up you route file web.php
located in /routes/
pasting inside this:
require base_path('routes/notifications.php');
Clear the route cache of your Laravel application:
php artisan route:cache
php artisan route:clear
Then you can run a migration:
php artisan migrate --path=/database/migrations/2024_09_06_000000_create_notifications_table.php
Or you can simply execute the SQL query (MySQL) in the file /database/migrations/notifications.sql
You are ready to go!
Include in you navigation or where you want your button the blade file:
@include('layouts.notifications')
And the notifications are all set up. To create a notification in a controller, include the model:
use App\Models\Notifications;
and use the function:
Notifications::notifiy('some text as notification');
- Choose the number of last notification to see:
In
app\Providers\NotificationServiceProvider.php
you will find this line of codechange to the desired number, default is 20.->limit(20)
- Change the receiver:
To change the receiver, you can configure the function
notify
in/app/Models/Notifications.php
for now will send a notification to all the users, so it will create a record for each user.$users = User::all();
- To mark a notification as read, you can simply click on it, this will hide the red dot.
- To hide "Unread notifications" mark all notifications as read.
- The style
notifications.blade.php
view was made using Bootstrap and FontAwesome, so the style will not be visible unless you include them. Alternatively, you can use you own styling by editing the.css
and the view.