How to add Toastr Notification in Laravel 10

How to add Toastr Notification in Laravel 10

Introduction

A fully functional and interactive web program needs a system that notifies or warns users about any existing errors or updates. This helps your audience engage with and use your app better. Did you know that you just need a few lines of simple code to integrate Toastr notification in Laravel 10 syntax to transform the functionality and quality of your website?

Our comprehensive guide will show you how to utilize this lightweight library to enhance your application’s overall user experience. In this tutorial, you will go through some easy steps, revealing certain codes that will help you insert Toastr notifications in Laravel 10. Get ready to bring aesthetic appeal to your platform with customizable notifications that match well with your website’s theme and improve its usability.

Toastr is a Javascript library for non-blocking notifications. Let's see how to implement toastr in laravel :

#Install

Install the toastr package using composer :  

composer require yoeunes/toastr

 

#Usage

toastr()->success('This works!!');

This is a success notification with no title.

 

#Example

namespace App\Http\Controllers;

use App\Models\Enquiry;

use Illuminate\Http\Request;

use Exception;

 

class EnquiryController extends Controller

{

    function store(Request $request){

        $request->validate([

            'name'=>'required',

            'phoneNumber'=>'required|numeric|digits:10',

            'message'=>'required',

        ]);

 

try{

        $enq = new Enquiry();

        $enq->name  = $request->input('name');

        $enq->phoneNumber  = $request->input('phoneNumber');

        $enq->message  = $request->input('message');

        $enq->save();

 

        toastr()->success('Submitted, you will be contact shortly..'); 

        return redirect()->back();

}

catch(Exception e){

        toastr()->error('Submitted, you will be contact shortly..');

        return redirect()->back();

}

    }

}

 

#Other Notification Usage

toastr->warning('This is a warning toast','Warning');

//This is a Warning toast with title

toastr->success('This is a warning toast', 'Success');

//This is a Success toast with title

toastr->error('This is a warning toast', ''Error);

//This is a Error toast with title

toastr->info('This is a warning toast', 'Info');

//This is a Info toast with title

toastr()

    ->info('hi there)

    ->success('this is success');

// You can also chain multiple messages together as per you need.

Conclusion

Now you have the tools for adding Toastr notification in your Laravel 10 PHP programming. Use the simple set of codes enclosed in this manual to easily insert a system to warn, instruct, or inform your application users.

This package is simple to use and if you want to modify it you can publish this package and a file will be created in the config folder name 'toastr.php' there you can make changes as per your needs.

You can go through the documentation if you want to deep dive in toastr.js.

4 Comments

ss

d

d

d

dss

ds

abc

not working

Leave a comment