Laravel: Display Flash Message after Registration or Log in in Blade template
Laravel Flash Message: in this lesson, I will show you to display success and error message using the flash message in Laravel web application and Laravel project. if you use Laravel framework and set message use session and display the message in the blade template. So this tutorial will more helpful for you, follow the example and source codes.
There has the different type of displaying flash in a Laravel web page, which message you display to the user success work. In this tutorial, I will show you how to show a success or error message when a user registration successful. Display message at the user login time so, you need to use flash-message in your web application for displaying the message.
Using Session::flash() message in your web application you can display the different type of message to the users. When you set any message in session flash-message, you need to use session under the namespace. The session is a library function, it will use in the parent constructor that’s like:
Use Session:
<?php namespace App\Http\Controllers; use Session; ?>
I will give you an example in the below of this text, how to set the message in the session flash-message. Then set the redirect page, when user registration or log in done. Before set redirect URL link, you need to set your text message in the session variable. Follow the example and try in your project.
Set Display Message:
<?php if($data==true){ Session::flash('flash_message','Successfully work done.'); return redirect('page_url'); }
Now your redirect page, display the message use Session::get(”). The session gets catch the data until the session variable broke or page reload.
Session Get:
<div> {{ Session::get('flash_message') }} </div>
If you want to check, if the session value is or not in the session variable, you can follow this example. This example helps you to check before display flash-message in your page, where you display the alert message. So you need to use Session::has(”) in the if condition for example:
Session Has:
<div> @if(Session::has('success_message')) {{ Session::get('success_message') }} @endif @if(Session::has('error_message')) {{ Session::get('error_message') }} @endif </div>