If you want to show stylish notifications like success, error, or warning messages in your Laravel 12 app, then Toastr is a great option. In this tutorial, I’ll show you how to add Toastr notifications step by step in a very simple and easy way. We’ll use session messages and Toastr.js to display alerts that look clean and modern.
Laravel 12 Toastr Notification: Step by Step Guide
Install laravel 12 using the following command.
composer create-project laravel/laravel laravel12-toastr
Include the following CDN links in your main Blade file (like resources/views/layouts/app.blade.php
or welcome.blade.php
):
<!-- Toastr CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" rel="stylesheet"/>
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Toastr JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
Now, in your controller, you can set a session flash message like this:
return redirect()->back()->with('success', 'Data saved successfully!');
In your main layout file or view, add the following script block to show notifications based on the session message:
<script>
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": true,
"progressBar": true,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
};
@if(Session::has('success'))
toastr.success("{{ Session::get('success') }}");
@endif
@if(Session::has('error'))
toastr.error("{{ Session::get('error') }}");
@endif
@if(Session::has('info'))
toastr.info("{{ Session::get('info') }}");
@endif
@if(Session::has('warning'))
toastr.warning("{{ Session::get('warning') }}");
@endif
</script>
Let’s say you have a form submission. After saving data in your controller, redirect with a success message:
public function store(Request $request)
{
// Save your data logic
return redirect()->back()->with('success', 'Form submitted successfully!');
}
Customizing Position
You can change the position by modifying positionClass
:
toast-top-right
toast-top-left
toast-bottom-right
toast-bottom-left
toast-top-full-width
toast-bottom-full-width
Output:
You will see a nice green success alert at the top-right of your page. You can also use error, info, or warning messages the same way.
You might also like: