Laravel Implement Flash Messages Example

 

In Laravel, flash messages are a convenient way to provide feedback or notifications to users after specific actions, such as form submissions or updates. Flash messages are typically displayed once and then cleared from the session data.


Here's a step-by-step example of how to implement flash messages in Laravel:


Step 1: Install Laravel (if not already installed)


If you haven't already set up a Laravel project, you can create one using Composer:


composer create-project laravel/laravel your-project-name


Step 2: Create a Route and Controller Method


Let's create a simple route and a controller method to demonstrate flash messages. Open the `web.php` file in the `routes` directory:


Route::get('/flash', 'FlashController@index');


Next, create a controller called `FlashController`:


php artisan make:controller FlashController


In the `FlashController.php` file, add a method called `index`:


use Illuminate\Http\Request;


class FlashController extends Controller

{

    public function index()

    {

        return view('flash-message');

    }

}


Step 3: Create a Blade View


Create a Blade view file named `flash-message.blade.php` in the `resources/views` directory. This view will display the flash message if one exists:


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Flash Message Example</title>

</head>

<body>

    @if(session('success'))

        <div class="alert alert-success">

            {{ session('success') }}

        </div>

    @endif


    <form action="{{ route('flash.submit') }}" method="POST">

        @csrf

        <button type="submit">Show Flash Message</button>

    </form>

</body>

</html>


This view checks if a flash message with the key `'success'` exists in the session. If it does, it displays a success alert.


Step 4: Create Another Route and Controller Method for Flash Message Submission


Add a route and controller method to handle the submission of the flash message:


In `web.php`:


Route::post('/flash', 'FlashController@submit')->name('flash.submit');


In `FlashController.php`:


public function submit()

{

    session()->flash('success', 'Flash message displayed!');

    return redirect()->route('flash.index');

}


In this code, we're using `session()->flash()` to store the flash message in the session with the key `'success'`. After that, we redirect the user back to the `flash.index` route.


Step 5: Run the Application


You can now run your Laravel application:


php artisan serve


Visit `http://localhost:8000/flash` in your browser. When you click the "Show Flash Message" button, the flash message will be displayed, and it will disappear when you reload the page or navigate to another page.


That's it! You've implemented flash messages in a Laravel application. You can use this concept for various notifications throughout your project, enhancing the user experience.