Laravel 12 Toastr Notification: Step by Step Guide

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

Laravel 12 Toastr Notification: Step by Step Guide

 

Step 1: Install Laravel 12

Install laravel 12 using the following command. 

composer create-project laravel/laravel laravel12-toastr

 

Step 2: Add Toastr CSS and JS

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>

 

Step 3: Add Flash Messages in Controller

Now, in your controller, you can set a session flash message like this:

return redirect()->back()->with('success', 'Data saved successfully!');

 

Step 4: Show Toastr Notification in Blade File

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>

 

Step 5: Example Use Case

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:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS