Notification Message show using a toastr in Laravel 7

When you are working on the live project you have to display a notification message on your screen so the user can show the display output what should have to they do.

That’s why jQuery plugin represents one of the easiest and best plugins which helps to display the notifications message on our screen.

So let’s start our tutorials


Step 1: Create a function in helper file

So open your helper.php file and put the below code.

<?php
function notificationMsg($text, $message){
    \Session::flash($text, $message);
}

Step 2: Changes in your Controller.

Now open your UserController.php file and put the below code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller{
	public function index(){
		$users = User::paginate(10);
		notificationMsg('success', "User View Load!");
		return view('users',compact('users'));
	}    
}

Step 3: Changes in the route file.

Open your web.php file and put the below code.

Route::get('/users', 'UserController@index')->name('users');

Step 4: Changes in blade file.

Open your user/index.blade.php file and put the below code.

@extends('layouts.app')
@section('content')
	<div aria-live="polite" aria-atomic="true" style="position: relative;">
        <div class="toast" style="position: fixed; top: 10px; right: 10px; z-index: 9999" data-delay="3000">
            <div class="toast-body bg-secondary">
                <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <i id="icon-arrow" class="fa fa-check"></i>
                {!! session('success') !!}
        	</div>
		</div>
	</div>
	<table class="table table-bordered">
	    <thead>
	        <tr>
	            <th>Name</th>
	            <th>Action</th>
	        </tr>
	    </thead>
	    <tbody>
	        @if(!empty($users))
	            @foreach($users as $user)
	                <tr>
	                    <td>{{ $user->name }}</td>
	                    <td>
	                        <button class="btn">Delete</button>
	                    </td>
	                </tr>
	            @endforeach
	        @else
	            <tr>
	                <td>There are no data.</td>
	            </tr>
	        @endif
	    </tbody>
	</table>
	{!! $users->links() !!}	
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	   <script>
	   jQuery(document).ready(function(){
	       @if(Session::has('success') && !empty(session('success')))
	           jQuery('.toast').toast('show');
	       @endif
	   });
	</script>
@endsection

So, Here is the tutorial is ready. now, open your tutorial and put the below URL:

http://127.0.0.1:8000/users

About Dhaval Shah

My name is Dhaval Shah. I'm a PHP developer, entrepreneur and CEO of DAG inventions PVT ltd. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress and Bootstrap.
Follow me on Github / Linkedin

1 Comment

Leave a Reply

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

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!