Laravel Flash Message Example

AuthorSumit Dey Sarkar

Pubish Date23 May 2023

categoryLaravel

In this tutorial we wil learn how to Laravel 8 flash message tutorial example.

 

Laravel Flash Message Example

 

Overview

1)  Set Up Routes

2) Create a Blade View

3) Create a Controller

4) Update the Route

5) Run the Application

 

How to set flash message in Laravel?

Flash messages are brief messages that are saved in the session and are presented to the user for a little duration.When a user completes an activity, such as completing a form successfully or running into an error, they are commonly used to send feedback or notifications to the user.

 

Step 1: Set Up Routes

Open routes/web.php file and define a route for displaying the flash message. For example:

Route::get('/flash-message', function () {
    return view('flash-message');
});

 

Step 2: Create a Blade View

Create a new file resources/views/flash-message.blade.php and add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel - Implement Flash Messages with example</title>
</head>
<body>
    @if(session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger">
            {{ session('error') }}
        </div>
    @endif

    <h1>Flash Message Example</h1>
    <a href="/flash-message">Show Flash Message</a>
</body>
</html>

 

Step 3: Create a Controller

Next, run the following command in your terminal to create a new controller: 

php artisan make:controller FlashMessageController

 

Open FlashMessageController.php file in the app/Http/Controllers directory and then replace its code with the below code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FlashMessageController extends Controller
{
    public function showMessage(Request $request)
    {
        // Flash success message
        $request->session()->flash('success', 'Flash message example - success.');

        // Flash error message
        $request->session()->flash('error', 'Flash message example - error.');

        return view('flash-message');
    }
}

 

Step 4: Update the Route

Update the routes/web.php file to use the controller method instead of the closure:

use App\Http\Controllers\FlashMessageController;

Route::get('/flash-message', [FlashMessageController::class, 'showMessage']);

 

Step 5: Run the Application

Run the following command in your terminal to view output:

php artisan serve

 

You should see a flash message example and a link to display the flash message if you browse to http://localhost:8000/flash-message in your web browser.

 

When you click the link, the flash messages will be displayed on the page, demonstrating the usage of flash messages in Laravel.

 

Note: Before completing these steps, ensure that Laravel is installed correctly and configured.

 

That's it! You now have a basic example of how to use flash messages in Laravel. You can customize the flash message content and styling to fit your needs.

 

Comments 0

Leave a comment