laravel 7 implement custom flash message with example – onlinecode
In this post we will give you information about laravel 7 implement custom flash message with example – onlinecode. Hear we will give you detail about laravel 7 implement custom flash message with example – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this tutorial, We will explain to you how to Implement Custom Flash Messages with Example in laravel. so we will create a custom flash-message using bootstrap without any package.
When the user performs any operation that time it happened a success or fails, We use Custom Flash Messages to show that. so you can follow the below step.
Create flash messages view filehere, in this step, we will create a flash-message.blade.php file. In this file we will create different alert flash-message codes like success, error, warning, info, and validation error.resources/views/flash-message.blade.php
@if ($message = Session::get('success')) <div > <button type="button" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('error')) <div > <button type="button" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('warning')) <div > <button type="button" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($message = Session::get('info')) <div > <button type="button" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> @endif @if ($errors->any()) <div > <button type="button" data-dismiss="alert">×</button> Please check the form below for errors </div> @endif
Use flash-message file in Master fileIn this step, we will include a flash-message.blade.php file in the master file. so you can see the below code.
<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <!-- Styles --> <link href="/css/app.css" rel="stylesheet"> </head> <body> <div > <div id="app"> @include('flash-message') @yield('content') </div> </div> <!-- Scripts --> <script src="/js/app.js"></script> </body> </html>
Use flash messages with redirectNow, you can pass different types of error messages in the controller file. so you can see below different types of error messages.
Redirect with Success Flash Message
return redirect()->route()->with('success','here your success message');
Redirect with error Flash Message
return redirect()->route()->with('error','here your error message');
Redirect with warning Flash Message
return redirect()->route()->with('wanrning','Here your warning message');
Redirect with info Flash Message
return redirect()->route()->with('info','Here your info message');
Redirect with Validation Error Flash Message
public function store(Request $request) { $request->validate([ 'txtFirstName'=>'required', 'txtLastName'=> 'required', 'txtAddress' => 'required' ]); ..... }
Hope this code and post will helped you for implement laravel 7 implement custom flash message with example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs