Laravel 9 Flash Message Example

WebSolutionStuff | Dec-27-2022 | Categories : Laravel

In this article, we will see a laravel 9 flash message example. Here, we will learn how to create or implement flash messages in laravel 7, laravel 8, and laravel 9. Also, you can pass the alert messages from the controller. Also, you can redirect with a success flash message, and redirect with an error message. 

So, we will see different types of alert message notifications like success messages, warning messages, error messages, info messages, etc. Also, we will use bootstrap flash message.

For display notifications, you can use different bootstrap classes like alert-success, alert-warning, alert-info, and alert-danger.

laravel 9 flash message example, flash message in laravel 7/8/9, redirect with flash message laravel 9, laravel 9 alert message, laravel 9 success message

Step 1: Create a File for Flash Message

In this step, we will create a flash_message.blade.php file. So, this file can include anywhere in the project. In this file, we will use session and check message types like success, error, warning, and info. if the session and message type match alert message will be displayed.

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: Include Flash Message File

In this step, we will use the flash_message.blade.php file in the default layout file.

@include('flash_message')

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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/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 of Flash Message

In this step, we will use flash messages with a back() function. So, add the following code to the controller file.

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

	$users = User::create($request->all());

	return back()->with('success','User created successfully');
}

Now, we will use the redirect() function with an error message.

public function create(Request $request)
{
    return redirect()->route('index')
        ->with('error','Something want wrong!');
}

Return redirect with route() with info message.

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

    $users = User::create($request->all());

    return back()->with('info','Duplicate email address!');
}

 


You might also like:



Recommended Post


Featured Post






Follow us
facebooklogo github instagram twitter