Laravel notifications with Notify.js without any package

Today we are going to use Notify.js for showing notifications for regular errors, warnings, infos and success messages in Laravel project.

We don\’t need any extra package to add this in our project. you can just copy and paste the code below and that is all. You just have to set the message and it will be visible automatically as soon as the page load.

To implement this we are going to use Notify.js library. This library will do everything by it self. So lets get started.

Download Notify.js

\"\"

First of all we have to download the Nofify.js library and place it in our public/js directory.
Please go to below mentioned link and download the minified version of the library.

https://notifyjs.jpillora.com/

After downloading and placing this library in the public/js directory. You have done 50% of your job.

Now lets use the library in our Laravel project. We need these notifications globally in our project so we can show them on any page.
To implement this we will create a new blade file for these notifications in the layout folder.

Create a new file notifications.blade.php in your resources/views/layouts directory and put the below mentioned code in the file.

<script>
    @if(Session::has(\'success\'))
        $.notify(\"{{ Session::get(\'success\') }}\", \"success\");
        @php
            Session::forget(\'success\');
        @endphp
    @endif
    @if(Session::has(\'info\'))
        $.notify(\"{{ Session::get(\'info\') }}\",\'info\');
        @php
            Session::forget(\'info\');
        @endphp
    @endif
    @if(Session::has(\'warning\'))
        $.notify(\"{{ Session::get(\'warning\') }}\", \"warn\");
      @php
        Session::forget(\'warning\');
      @endphp
    @endif
    @if(Session::has(\'error\'))
        $.notify(\"{{ Session::get(\'error\') }}\", \"error\");
        @php
            Session::forget(\'error\');
        @endphp
    @endif
</script>

Currently we haven\’t included Notify.js library in our project. So lets do it now. You can include it in the header or footer of your project but I prefer in the same file.

So lets include the Notify.js library on the top of notifications.blade.php file.

<script src=\"{{ asset(\'js/notify.js\') }}\"></script>

Now include this notifications.blade.php file in your main layout file or any of the view where you want to show the notifications like we did in the example code below.

 </head>
   <body>
        @include(\'layouts.notifications\')
      <!---Wrapper Start-->
      <div class=\"wrapper\">

That\’s it. We are done for setting up notifications for our project. Now from the controller lets setup a notification like mentioned below.

//Do what you want here
session()->put(\'error\',\'Set error message like this.\');
return redirect()->route(\'home\');

Set other messages like mentioned below.

session()->put(\'success\',\'Set success message like this.\');
session()->put(\'info\',\'Set info message like this.\');
session()->put(\'warning\',\'Set warning message like this.\');
\"\"

Hope this article will be helpful. Thank you for visiting .

You may also like...