Notification Message show using a toastr in Laravel 6

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 plugin 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\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\User;
use Spatie\Permission\Models\Role;
use DB;
use Hash;

class UserController extends Controller{
    public function index(Request $request){
    	$data = User::orderBy('id','DESC')->paginate(5);
        return view('Admin.users.index',compact('data'));
    }
    public function create(){
    	$roles = Role::pluck('name','name')->all();
    	return view('Admin.users.create',compact('roles'));
    }
    public function store(Request $request){
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required|email|unique:users,email',
            'password' => 'required|same:confirm-password'        
        ]);
        $input = $request->all();
        $input['password'] = Hash::make($input['password']);
        $user = User::create($input);
        // $user->assignRole($request->input('roles'));
        notificationMsg('success', "User Created Successfully!");
        return redirect()->route('users.index');
    }

Step 3: Changes in the route file.

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

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

Step 4: Changes in blade file.

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

@extends('layouts.app')
@section('content')
    <div class="row">
      <div class="col-lg-12 margin-tb">
          <div class="pull-left">
              <h2>Users Management</h2>
          </div>
          <div class="pull-right">
              <a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a>
          </div>
      </div>
    </div>
    <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>
    <!-- @if ($message = Session::get('success'))
      <div class="alert alert-success">
        <p>{{ $message }}</p>
      </div>
    @endif -->
    <table class="table table-bordered">
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Roles</th>
        <th width="280px">Action</th>
      </tr>
      @foreach ($data as $key => $user)
        <tr>
          <td>{{ $user->name }}</td>
          <td>{{ $user->email }}</td>
          <td>
            @if(!empty($user->getRoleNames()))
              @foreach($user->getRoleNames() as $v)
                 <label class="badge badge-success">{{ $v }}</label>
              @endforeach
            @endif
          </td>
          <td>
             <a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a>
             <a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a>
              {!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
                  {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
              {!! Form::close() !!}
          </td>
        </tr>
      @endforeach
    </table>
    <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://localhost:8888/users

EAT, SLEEP, CODING and Repeat! Happy Coding!

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. 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

5 Comments

  1. This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your great post. Also, I have shared your site in my social networks!

  2. You made some decent points there. I checked on the internet for additional information about the issue and found most individuals will go along with your views on this site.

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!