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, laravel 9, and laravel 10. 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/10, redirect with flash message laravel 9/10, laravel 9/10 alert message, laravel 9/10 success message laravel 10 flash message example, how to display an alert notification in laravel 10, and bootstrap alert notification in laravel 10.

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
Laravel 9 Flash Message Example
Laravel 9 Flash Message Exampl...

In this article, we will see a laravel 9 flash message example. Here, we will learn how to create or implement flash mes...

Read More

Dec-27-2022

How To Image Upload With Progress Bar Angular 15
How To Image Upload With Progr...

As an Angular 15 developer, I understand the importance of incorporating image upload functionality with a progress bar...

Read More

Jun-21-2023

Laravel 9 Many To Many Relationship Example
Laravel 9 Many To Many Relatio...

In this article, we will see laravel 9 many to many relationship example. Also, you can use many to many relationsh...

Read More

Apr-03-2022

How To Redirect Another Page Using Javascript
How To Redirect Another Page U...

In this article, we will see how to redirect another page using javascript. we will redirect the page to another pa...

Read More

Nov-04-2022