Laravel 8 Toastr Notification Tutorial
Toastr is a popular JavaScript library used to display notifications like info, success, warning, and error in a sleek and user-friendly way. It's beneficial after actions such as user registration, form submission, or any other interactive feature.
In this tutorial, we’ll learn how to use Toastr in a Laravel 8 project.
Step 1: Install Toastr Package
Install the Laravel Toastr wrapper via Composer:
Laravel 5.5 and above: No need to register the service provider or alias manually. It uses package auto-discovery.
For Laravel <= 5.4
If you're using Laravel 5.4 or below, add the following to your config/app.php
:
Service Provider:
Alias (Optional):
Step 2: Publish Configuration (Optional)
To customize the Toastr settings (like position, timeout, etc.), publish the config file:
Choose the config for brian2694/laravel-toastr
.
Step 3: Add Toastr Assets to Blade Layout
Add the required CSS and JS for Toastr in your main layout file (layouts/app.blade.php
or similar).
Inside <head>
tag:
Before the closing </body>
tag:
📝
Toastr::message()
will render the flash message from the controller into the frontend as a toast notification.
Step 4: Use Toastr in Controller
Example usage in your controller to show a success message:
You can also use:
-
Toastr::info('Info message', 'Title');
-
Toastr::warning('Warning message', 'Title');
-
Toastr::error('Error message', 'Title');
Optional Customization
You can modify the default appearance and behavior of the notifications by editing the published config file:
Output Preview
When a post is successfully created, you’ll see a toast notification like this:
Post added successfully 🙂