Top Menu

Here we discuss how to add toaster notification in any version of laravel application.

In this post, we will look at how we can utilize any version of Laravel’s flash helper function to show toaster notifications in our application.
When user signup or sign in on your website, or perform an action, it’s always recommended to notify the user about progress from a UX point of view.
Laravel provides flash() helper to set the notification message which will be available on the next HTTP request and then deleted. You can store notifications in a session using the below method.
TestimonalsController

namespace AppHttpControllers;
use AppTestimonals;
use IlluminateHttpRequest;
use IlluminateSupportFacadesInput;
use IlluminateSupportFacadesFile;
use IlluminateSupportFacadesUrl;
use IlluminateSupportFacadesDB;
use InterventionImageImageManagerStatic as Image;
use Auth;
use CarbonCarbon;
class TestimonalsController extends Controller
{
public function create(Request $request)
{
$request->session()->flash('success', 'Post created successfully.');
return redirect('testemonal-list')->with('success',"Yout Custome notification");
}
}

Then you can access the session data using the below method and display it to the user.

Content@if(session('success')) toastr.options.positionClass = 'toast-center-center'; toastr.success("{!!session('success') !!}"); @elseif(session('error'))

Instead of using the above method, I rely on using Toastr notification to show messages. Toaster is a javascript library to show notifications in a modern way.
First thing first, you need to include the Toastr library to Laravel blade views. You can achieve this by the below methods.
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:

 

Then you can access the session data using the below method and display it to the user.

Content@if(session('success')) toastr.options.positionClass = 'toast-center-center'; toastr.success("{!!session('success') !!}"); @elseif(session('error'))

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Close