How to implement custom flash message in laravel with example

How to implement custom flash message in laravel with example

Hello guys, In this article i am going to tell you how can we implement custom flash message in laravel with different types of examples. Flash messages are very important things to show a message after performing any operation in any web application.

A user can be notified that the operation he is performing has completed or not by flash messages. Flash messages can be of info, warning, error, success types.

I am assuming you have already a working laravel project or you have set up laravel project successfully on your system.

Let’s create custom flash message for our laravel web application. We will see how can we use flash messages for different situation with some examples.

Step 1: Create flash messages view file

In this step we will create a blade templete or view file to store all the messages, let’s create a file and give a name to it as flash-message.blade.php and copy the code into this file as given below.

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>	
  Error Occurred. Please look into the form.
</div>
@endif

Step 2: Include Flash Message in Master Page

Now, after creating flash message file with messages, we will add flash message in master layout by which these messages could be available in all pages. Let suppose we have master page file exists on this path resources/views/layouts/app.blade.php. 

Now open this layout file and add the code to include flash message file like below.

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">
    <!-- Include Styles -->
    <link href="/css/app.css" rel="stylesheet">
</head>
<body>


    <div id="app">
        @include('flash-message')


        @yield('content')
    </div>


    <!-- Include Scripts -->
    <script src="/js/app.js"></script>
</body>
</html>

Step 3: Use of Flash Messages

We have successfully created template or biew for flash message in laravel and added it into master page layout. Now we comes to the point where we  will actually use this flash messages.

Here, for example i am taking only a method to show how we will use send different type of flash message from controller to the view.

Use Case for Success Flash Message 

return redirect()->route()->with('success','Write your message for success');

Use Case for Info Flash Message

return redirect()->route()->with('info','Write your info message');

Use Case for Warning Flash Message 

return redirect()->route()->with('wanrning','Write your warning message here');

Use Case for Error Flash Message

return redirect()->route()->with('error','Write your message here for displaying error');

We can also use these flash message for showing validation errors as it is written in flash message blade template file above.

Use Case for Validation Flash Message in Laravel

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


    .....
}

We will only need to validate in the controller, if validation fails laravel automatically send back to the form with error message and we have already handled to show these error messages in falsh message in laravel view file.

Best thing of this custom flash message is that we can change the layout and design or look of the flash messages anyttime. We will just need to change into flash message blade file. We can also use this flash messages for ajax.

You can also read the article to get current route with parameter in laravel 

Laravel get current route with parameters example