Laravel Tutorials - How to create Session Flash Messages in Laravel

How to create Session Flash Messages in Laravel

2020-03-08 Lukas Markevičius
How to create Session Flash Messages in Laravel featured image

Today we are going to continue upgrading our blog application. If you missed the previous tutorial or want to just catch up you can follow Laravel Image Upload Tutorial and then come back. If you want to get a source code to begin with you can get it from here.

If you wish to follow along clone the repository and in your terminal run:

$ git checkout create-images -b <your-branch-name>

For now we can create, edit or delete posts and also upload images. But after all these actions we don't get any kind of message saying that the process was successfully completed. We only can assume that everything ran smoothly if we get redirected to another page or don't see any server errors. So for this scenario we would want to get some sort of a message which would be displayed to the user. But we also don't want to put this information to the database or get an email or save it anywhere else. That's why in this tutorial we will be using Session flash messages.


First of all, we will write our HTML code to display flash messages. We need to put this code in every place where we want to see the message. In our case, we need to put this code in create, edit, show and index.blade.php files inside our resources/views/post folder. Right after our navigation we put:

...
@if (Session::has('success'))
    <div class="alert alert-success alert-dismissible fade show" role="alert">
        <h4 class="alert-heading">Success!</h4>
        <p>{{ Session::get('success') }}</p>

        <button type="button" class="close" data-dismiss="alert aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
@endif

@if (Session::has('errors'))
    <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <h4 class="alert-heading">Error!</h4>
            <p>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </p>

            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
    </div>
@endif
...

As you can see we are using if statement which checks if session has a success or errors messages. If it has success message we simply return it to the user. If it has errors then we need to make foreach loop to display every single one error to the user.
 
Now all we need to do is write those messages so that when a certain action is completed it would show up to the user. So in PostController.php we add our flash messages to store, update and destroy functions right before returning user to the next page:

...
Session::flash('success', 'You have successfully updated a post!');

return redirect()->route('post.show', $post->slug);
...

As you can see we used Session::flash method which accepts two parameters. The first one is the name of the message and the second is the message itself. Also don't forget to include Session class in the beginning of the controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Post;
use Image;
use Storage;
use Session;
...

It's important to know that the error messages are generated automatically by Laravel and it will show up when the validation doesn't pass some sort of the rule. Also using the same method you could make different flash messages for warnings, info and etc.
 
So that's it! You now have learned how to create session flash messages in Laravel. If you have any more questions please feel free to comment down below.
 
Also if you would like to get a complete source code of this tutorial you can get it from here. Don't forget to checkout to session-messages tag:

$ git checkout session-messages -b <your-branch-name>
How to create Session Flash Messages in Laravel pinterest image

Related Tutorials

How to Generate a Simple XML Sitemap using Laravel

Posted 2020-03-08 Lukas Markevičius

Today we are going to learn how to generate a simple XML sitemap using Laravel. Mainly you can create it either manually or create some sort of automated solution. To create it manually you can make y...

How to Create Slugs in Laravel

Posted 2020-03-27 Lukas Markevičius

Today we are going to learn how to create slugs in Laravel. You are going to learn: Why use slugs How to create slugs How to show objects with the slugs How to edit slugs Why use Slugs If yo...

Back