How To Use Laravel 8 Flash Message with Bootstrap Tutorial

Share this Article
Reading Time: 4 minutes
426 Views

Flash message in laravel is a key factor to display some messages to user. There are several types of flash message notification bootstrap category classes like alert success, alert danger, alert info, alert warning messages.

Inside this article we will see the complete detail about all available flash message notification bootstrap classes. We will see in detailed concept and their usage.

Learn More –

This article will help you to understand about laravel 8 flash message with bootstrap concept.

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Set Flash Message From Controller

Session Facade is used to set flash message.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class SiteController extends Controller
{
    public function showNotifications()
    {
        Session::flash('success', 'This is a success message');
        Session::flash('error', 'This is an error message');
        Session::flash('info', 'This is an info message');
        Session::flash('warning', 'This is a warning message');

        return view("notification");
    }
}

Illuminate\Support\Facades\Session is Session facade package.

Session::flash(key, value); is used to set flash message

success, error, info, warning are keys which we can access into layout files to print flash messages.


Get Flash Message at Layouts

To print flash messages first we need to check, flash message key exists or not. If exists then print else no message will be displayed.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Laravel 8 Flash Message Tutorial</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/>
</head>
<body> 
 
<div class="container" style="margin-top:50px;">

    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>    
        <strong>{{ $message }}</strong>
    </div>
    @endif
      
    @if ($message = Session::get('error'))
    <div class="alert alert-danger alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>    
        <strong>{{ $message }}</strong>
    </div>
    @endif
       
    @if ($message = Session::get('warning'))
    <div class="alert alert-warning alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>    
        <strong>{{ $message }}</strong>
    </div>
    @endif
       
    @if ($message = Session::get('info'))
    <div class="alert alert-info alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>    
        <strong>{{ $message }}</strong>
    </div>
    @endif
</div>

</body>
</html>

@if ($message = Session::get(‘error’)), by the help of this code we check that message key like error exists or not.


Add Route

Open web.php from /routes folder. Add given route into it.

//..

use App\Http\Controllers\SiteController;

Route::get("notification", [SiteController::class, "showNotifications"]);

//..

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL – http://127.0.0.1:8000/notification

We hope this article helped you to learn How To Use Laravel 8 Flash Message with Bootstrap Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Leave a Comment