How to add Toastr Notification in Laravel 10

How to add Toastr Notification in Laravel 10

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

<?php

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.

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