First thing, you need to include the Toastr library to Laravel blade views. You can achieve this by below methods.
1.Include css and js file from a CDN
2.Install the library using NPM
3.Download the css and js file from github Toastr Repository
However you choose the grab the library, you have to add the location to the 2 files in the head tag of your master layout file like so:
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css">
After including the library, whenever you need to redirect to a page most probably from a controller, you can pass the notification information like so:
$notification = array(
'message' => 'Post created successfully!',
'alert-type' => 'success'
);
return Redirect::to('/')->with($notification);
After adding redirect notification to your controller, add following javascript at the bottom of your layout file (app.blade.php or master.blade.php – depends on how you name it).
@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
Note* Please open and close script tag before and after the above code snippet respectively.
That’s it. Now if you visit or perform the action you have targeted, you will be shown a nice notification using toastr. You can set the alert type from info, warning, success or error and correct colored notification will be shown.