clock  Mon - Sun 8.00 AM - 8.00 PM
fb
instagram
play store
pinterest

Laravel Flash Message Example

writter  Sumit Dey Sarkar
Date  23 May 2023
Language  Laravel
Laravel Flash Message Example

Laravel Flash Message Example

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

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website