Laravel Laravel 6 Laravel 7

Adding Toastr Notifications to your Laravel App

Pinterest LinkedIn Tumblr

However you decide to include it in your project you will then need to add the location to the 2 files in the <head> tag of your master layout file like so:

<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">

Then whenever you need to redirect to a page and pass a notification you can pass the redirect a notification array like so:

$notification = array(
	'message' => 'I am a successful message!', 
	'alert-type' => 'success'
);

return Redirect::to('/')->with($notification);

Then at the bottom of your master layout file (master.blade.php or app.blade.php) you will need to include the following javascript to show the notification type and the message you want to show:

<script>
  @if(Session::has('message'))
    var type = "{{ Session::get('alert-type', 'info') }}";
    switch(type){
        case 'info':
            toastr.info("{{ Session::get('message') }}");
            break;
        
        case 'warning':
            toastr.warning("{{ Session::get('message') }}");
            break;

        case 'success':
            toastr.success("{{ Session::get('message') }}");
            break;

        case 'error':
            toastr.error("{{ Session::get('message') }}");
            break;
    }
  @endif
</script>

And it’s as easy as that. Make sure in the $notification array from step 2 that you specify whether you want the message to be an alert-type of infowarningsuccess, or error and the correct color notification will be shown.

Read More: https://devdojo.com/devdojo/toastr-notifications-in-your-laravel-app

Write A Comment

Open chat
Hey Visitor,

How can I help you?