How to Show Flash Message in Laravel ?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Flash Message with Laravel Tutorial

Hi Dev’s, Today now, I will give you a example of how to use redirect also with flash messages without any package in your Laravel application. So now in this example i am not use any flash messages package in our example.

So here flash messages is required in laravel application because on that way we can easily give alter with what progress complete, success, warning etc. Now in this example we will use bootstrap because alert flash message design available in bootstrap.

So now in this blog i am added flash message look like redirect also with success message, error message, warning message and also info message.

Step 1 : Create flash-message blade file

Now in this step we will create a flash-message.blade.php file. So in this file we will write code of alert bootstrap and check which messages come.

There are following alert will added:

1)success

2)error

3)warning

4)info

5)validation error

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 flash-message.blade.php file in our theme default file. So we can add file like this way:

resources/views/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">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha256-L/W5Wfqfa0sdBNIKN9cG6QA5F2qx4qICmU2VgLruv9Y=" crossorigin="anonymous" />
	<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha256-WqU1JavFxSAMcLP2WIOI+GB2zWmShMI82mTpLDcqFUg=" crossorigin="anonymous"></script>
</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

So now in this step we will show you how to give message when you redirect one by one:

1. Redirect with success message

Now here we can simple redirect route or redirect url or redirect back with success flash message. So we can just use in controller like this way:

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

	$items = Post::create($request->all());

	return back()->with('success','Post created successfully!');
}

2. Redirect with error message

Also we can use on validation error redirect back with error then show error message.

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

Now we can simply redirect url or redirect with warning with flash message.

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

Again we can simple redirect route or redirect url or redirect back with info flash message.

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

    $posts = Post::create($request->all());

    return back()->with('info','You added new posts, follow next step!');
}

5. Validation Error

Also If we use laravel 8 validation then we can redirect back with errors automatically, So At that time it will also generate error flash message.

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

    .....
}

Read also : How To Set Bcc And Cc Mail Address In Laravel Mail?

I hope it will help you. Also you can follow us on Facebook

About 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.

View all posts by Shahriar Sagor →