how to use Flash Message using LARAVEL 10

Spread the love

Hello Guys,

Learn to Implement Flash Messages in Laravel 10 Discover how to effectively use flash message using Laravel 10 with this step-by-step guide. You’ll gain insights into creating flash message notifications and different types of alerts for your Laravel 10 projects, including success, danger, info, and warning messages. Whether your controller task succeeds or fails, we’ll show you how to display the appropriate flash message.

Flash messages play a crucial role in any Laravel 10 application, aiding in conveying progress, errors, warnings, and other vital information. This article covers multiple methods for displaying flash messages, such as redirecting with success, error, warning, or info messages. Additionally, we’ll demonstrate how to enhance your layout with a captivating bootstrap flash alert.

Integrating flash messages into your Laravel 10 application is a straightforward three-step process. Stay tuned for a detailed walkthrough!

So let’s proceed as follows the example of Flash Message using LARAVEL:

Step 1: Create a Global File For Flash Message

In our development process, we’ll begin by crafting a fresh Blade file named ‘flash-message.blade.php.’ Within this file, we’ll incorporate Bootstrap alert code to effectively manage and display incoming messages.

Also Read: How To Import Excel File Into Database in Python

Anticipate these forthcoming alerts:

1)success

2)error

3)warning

4)info

5)validation error

Learn how to create the flash-message.blade.php file and easily insert the provided code.

resources/views/flash-message.blade.php
@if ($message = Session::get('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
  <strong>{{ $message }}</strong>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif 
    
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <strong>{{ $message }}</strong>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
     
@if ($message = Session::get('warning'))
<div class="alert alert-warning alert-dismissible fade show" role="alert">
  <strong>{{ $message }}</strong>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
     
@if ($message = Session::get('info'))
<div class="alert alert-info alert-dismissible fade show" role="alert">
  <strong>{{ $message }}</strong>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
    
@if ($errors->any())
<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <strong>Please check the form below for errors</strong>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif

Step 2: Use Flash Message in Theme

Incorporate the flash-message.blade.php file into your default theme file in this step. Simply integrate this flash file into your standard theme blade file, as demonstrated below:

Also Read: How to install Docker on Windows 10/11 STEP BY STEP

@include('flash-message')

In addition, I’ve successfully integrated a Flash file into my website theme. You can follow this approach by checking out the following illustrative example:

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">
    <!-- Styles -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" ></script>
</head>
<body>
  
<div class="container">
  
    @include('flash-message')
  
    @yield('content')
  
</div>
  
    <!-- Scripts -->
    <script src="/js/app.js"></script>
</body>
</html>

Step 3: Use Flash Messages with Redirect

In this phase, you’ll learn effective individual communication strategies for redirection.

Also Read: How To Generate Invoice PDF In Laravel 10

1. Redirect with the success message

Discover effective methods for utilizing a controller to effortlessly manage route redirection, URL manipulation, and success message display.

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


    $items = Item::create($request->all());


    return back()->with('success','Item created successfully!');
}
2. Redirect with an error message

Learn how to effectively utilize a controller for route redirection, URL management, and seamless error flash message redirection.

public function create(Request $request)
{
    return redirect()->route('home')
        ->with('error','You have no permission for this page!');
}
3. Redirect with a warning message

Utilizing a controller offers versatile options for route redirection and URL management, including straightforward route redirection and seamless redirection with a prominent flash message.

Also Read: How to get .env variable in blade and controller in LARAVEL?

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

Discover the versatile applications of a controller to effortlessly manage route redirection, URL manipulation, or seamless redirection with informative content.

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


    $items = Item::create($request->all());


    return back()->with('info','You added new items, follow next step!');
}
5. Validation Error

When implementing Laravel 10 validation, you’ll experience instant redirection in case of errors, accompanied by the generation of an error flash message.

Also Read: How to Use LARAVEL DB Raw Query?

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

To enhance your Laravel 10 application, you can effortlessly incorporate flash messages using this method.

Discover how this can be beneficial to your project.

Uncover the ultimate insights in our collection, your gateway to the best Laravel blog content.

Leave a Comment