How to Implement Flash Messages in Laravel 8?

Today now in this post i will show you How to Implement Flash Messages in Laravel 8? Here i am going to share with you an example of how we can use redirect with the flash messages without using any package in Laravel application. So now in this example i needn’t use any package for the flash message. We can do this very simply by the following this tutorials for flash messages in laravel 6, laravel 7 and laravel 8 application.

We know that Flash messages is most need in any laravel application because on that way we can give alter with what progress complete, error, warning etc. Now in this tutorial we are the several way to give flash message as like as redirect with the success message, redirect with error message, redirect with warning message and redirect with info message. Now in this example i will use bootstrap flash alert layout on that way it become be good layout.

So, we need to just follow the basic three step to integrate the flash message in our laravel application. So let’s start and follow the bellow step:

Step 1: flash-message blade file

Now at the first step we need to create a new blade file as flash-message.blade.php. Now in this file i will write all the code of bootstrap alert and check which messages will come.

There are the following alert i am added:

1)success

2)error

3)warning

4)info

5)validation error

So, let’s start and create flash-message.blade.php file and put the bellow code on that file.

resources/views/flash-message.blade.php

@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 file in theme

Now in this step we need to just include the flash-message.blade.php file in our theme default file. So i will add file as like as on this way:

resources/views/layouts/app.blade.php

<!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

Now in this step i will teach you how to give the message when we redirect one by one:

1. Redirect with success message

i can simple redirect the route or redirect url or redirect back with the success flash message, we can also use this in controller as like this way:

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

i can simple redirect the route or redirect url or redirect back with the success flash message, we can also use this in controller as like this way:

public function create(Request $request)
{
    return redirect()->route('home')
        ->with('error','You have no permission for this page!');
}
3. Redirect with warning message

i can simple redirect the route or redirect url or redirect back with the success flash message, we can also use this in controller as like this way:

public function create(Request $request)
{
    return redirect()->route('home')
            ->with('warning','Don't Open this link);
}
4. Redirect with info message

i can simple redirect the route or redirect url or redirect back with the success flash message, we can also use this in controller as like this way:

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

i can simple redirect the route or redirect url or redirect back with the success flash message, we can also use this in controller as like this way:

public function create(Request $request)
{
    $this->validate($request,[
        'title' => 'required',
        'details' => 'required'
        ]);
    .....
}

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.