Laravel

Flash message in Laravel 8 -example

In this article we will learn about how we can create flash message in laravel 8

we will define various type of flash message notification like alert success, alert danger, alert info, alert warning messages in bootstrap laravel 8 projects. When you have success task on controller method then you can use success flash message, if you have any error task then you can use error flash message.

Flash messages are required in laravel 8 application because that way we can give alter with what progress complete, error, warning etc. In this tutorial, I added several ways to give a flash message like redirect with success message, redirect with an error message, redirect with a warning message and redirect with info message. In this example, we use a bootstrap flash alert layout that way it becomes good layout.

We are going to do following things in our article

  • Create a global file for flash message
  • Include that file
  • return redirect with flash message

1 . Create a global file for flash message

This file will include the following alerts

  • Success
  • Error
  • Warning
  • Info

So first create a folder named as alert under resources/views, under the alert folder we will create a blade file named as flash-message.blade.php, so you complete path will be like below this resources/views/alert/flash-message.blade.php

Write the below code in 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

2 . Include that file

Now you can include this file where you want to show the error messages, or you can also include that file in your app.blade.php file like the below one

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

3. Return redirect with a flash message

We can simply redirect route or redirect URL or redirect back with warning,error,success,info flash message, we can use in the controller like this way:

Redirect with success message

public function create(Request $request)
{
    return redirect()->route('home')
            ->with('success',"file successfully stored");
}

Redirect with error message

public function create(Request $request)
{
    return redirect()->route('home')
            ->with('error',"issue while upload");
}

Redirect with warning message

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

Redirect with info message

public function create(Request $request)
{
    return redirect()->route('home')
            ->with('info',"info message");
}

Read Also: Crud Application in Laravel

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button