Javatpoint Logo
Javatpoint Logo

Implement Flash Message with Laravel 5.7

In this section, we are going to use Laravel 5.7 so that we can use a custom bootstrap alert flash message. In our given example, we are going to use the controller method to redirect with an alert message, and then we will use a blade file to print the flash message. Our bootstrap Laravel 5.7 project will show some messages like an alert warning, alert danger, alert info, alert-success, etc. We can easily perform it without using any composer package. So, in our example, we will not use any package.

If we use the laravel 5.7 application, it will require the flash message because the flash message is used to provide an alert with the detail of complete progress, warning, and error, etc. The flash message can be provided in several ways like redirecting with info message, error message, success message, and warning message. In our example, we will make a good layout by using the bootstrap flash alert layout. In the laravel application, we can integrate flash message by using a step by step process, which is as follows:

Step 1:

In this step, we are going to Create Global Blade File for Flash Message. For this, flash-message.blade.php will be created, which is a new blade file. We will use this file to write bootstrap alert code, and we will check which messages come. For this, we are going to add some alerts, which are described as follows:

  1. success
  2. error
  3. warning
  4. info
  5. validation error

So we will create flash.message.blade.php file. After that, we will use the following code to add into the file:

resources/views/flash-message.blade.php:

Step 2:

In the second step, we are going to Include Flash message in Theme. For this, we will use our theme default file and then include flash-message.blade.php file into it, which is shown in the following code:

resources/views/layouts/app.blade.php

Step 3:

In the third step, we will Use flash message with Redirect. Here we will redirect one by one and provide a message on every redirect. The messages of redirection can be divided into various types.

1. Redirect with success message

If our project in Laravel needs to redirect url, redirect route, or redirect back, we can easily do that with the help of a success flash message. In order to do this successfully, we have to use a controller, which is described as follows:

Our above code is ready to run. When we run this code, we will get the below output of the success flash message like this:

Implement Flash Message with Laravel 5.7

2. Redirect with error message

If our project in laravel needs to redirect url, redirect route, or redirect back, we can easily do that with the help of an error flash message. In order to do this successfully, we have to use a controller, which is described as follows:

Our above code is ready to run. When we run this code, we will get the below output of the error flash message like this:

Implement Flash Message with Laravel 5.7

3. Redirect with warning message

If our project in Laravel needs to redirect url, redirect route, or redirect back, we can easily do that with the help of a warning flash message. In order to do this successfully, we have to use a controller, which is described as follows:

Our above code is ready to run. When we run this code, we are going to get the below output of the warning flash message like this:

Implement Flash Message with Laravel 5.7

4. Redirect with info message

If our project in Laravel needs to redirect url, redirect route, or redirect back, we can easily do that with the help of an info flash message. In order to do this successfully, we have to use a controller, which is described as follows:

Our above code is ready to run. When we run this code, we will get the below output of the info flash message like this:

Implement Flash Message with Laravel 5.7

5. Validation error

We will automatically redirect back to error if we want to use validation in laravel 5. After this, an error flash message will also be generated.

Our above code is ready to run. When we run this code, we will get the below output of the error flash message like this:

Implement Flash Message with Laravel 5.7

By using the above way in our laravel 5 application, we can easily implement flash messages.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA