In this tutorial you will learn about the Laravel 8 Custom Flash Message Tutorial Example and its application with practical example.
In this Laravel 8 Custom Flash Message Tutorial Example I will show you how to create and use flash message in laravel 8 application. In this tutorial you will learn to create and use flash message in laravel. While working with laravel application we come to situations where we want to show/display various flash success and error messages. In this article I will share various example to create and use various types of flash messages in laravel 8.
Laravel 8 Custom Flash Message Tutorial Example
In this Laravel 8 Custom Flash Message Tutorial you will learn how to create Custom Flash Message in laravel .In this step by step tutorial you will understand how to customize Flash message with example. Please follow the instruction given below:
Step 1: Create Global File For Flash Message
In this step we will create a global flash message file and add following alert in it:
1)success
2)error
3)warning
4)info
5)validation error
Let’s create flash-message.blade.php file and put bellow code on that file.
resources/views/flash-message.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
@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 @if ($errors->any()) <div class="alert alert-danger"> <button type="button" class="close" data-dismiss="alert">×</button> Please check the form below for errors </div> @endif |
Step 2: Use Flash Message in Theme
Now we will include global flash message file in app layout as following:
1 |
@include('flash-message') |
resources/views/layouts/app.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Styles --> <link href="/css/app.css" rel="stylesheet"> </head> <body> <div id="app"> @include('flash-message') @yield('content') </div> <!-- Scripts --> <script src="/js/app.js"></script> </body> </html> |
Step 3: Use Flash Messages with Redirect
we will learn here how to show message when you redirect one by one:
1. Redirect with success message
Below is example to simple redirect with success flash message.
1 2 3 4 5 6 7 8 9 |
public function create(Request $request) { $this->validate($request,[ 'title' => 'required', 'details' => 'required' ]); $items = Item::create($request->all()); return back()->with('success','Item created successfully!'); } |
2. Redirect with error message
Below is example to simple redirect with error flash message.
1 2 3 4 5 |
public function create(Request $request) { return redirect()->route('home') ->with('error','You have no permission for this page!'); } |
3. Redirect with warning message
Below is example to simple redirect with warning flash message.
1 2 3 4 5 |
public function create(Request $request) { return redirect()->route('home') ->with('warning',"Don't Open this link"); } |
4. Redirect with info message
Below is example to simple redirect with info flash message.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public function create(Request $request) { $this->validate($request,[ 'title' => 'required', 'details' => 'required' ]); $items = Item::create($request->all()); return back()->with('info','You added new items, follow next step!'); } |
5. Validation Error
1 2 3 4 5 6 7 8 |
public function create(Request $request) { $this->validate($request,[ 'title' => 'required', 'details' => 'required' ]); ..... } |