Chapter 7 - Laravel Flash Messages

Hello Artisan's, welcome to the 7th chapter of being an Artisanary. Her we'll discuss about Flash Messages. Flash messages are a good way to display feedback or notifications to the user after an action has been completed. They are typically used to display success or error messages after a form submission or other user action. So if you already complete the previous chapters/sections, then you're good to go, if not my recommendation would be please complete the previous chapters. Because we'll use the same old repository that we use from chapter 4.

Note: Tested on Laravel 10.0

Table of Contents

  1. Create a flash message form Controller
  2. Use a flash message in Blade file

Create a flash message form Controller

To create a flash message in Laravel, we can use flash messages in two way,

Example 1. using the with() method on the Redirect response

See the below source code which was we used in the previous chapter for fetching the list of blogs.

BlogController.php
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse
    {
        try {
            $data = [
                'blogs' => $this->blogRepository->all()
            ];
            return view('blogs.index', $data);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

Here in catch() method we use the with() for showing any error messages if we've.

Example 1. using the session()->flash() method

We can also use session()→flash() to show the flash messages. Let's look at the below source code

BlogController.php
public function index(): \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\View|\Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse
    {
        try {
            $data = [
                'blogs' => $this->blogRepository->all()
            ];
            session()->flash('success', 'Blog list Retrieved SUccessfully');
            return view('blogs.index', $data);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

Now everytime we'll see the success messages saying “Blog list Retrieved Successfully” whenever we visit the blog list page.

Use a flash message in Blade file

To show the flash message in blade file we'll use the bootstrap Alert Components. Let's see the below source code.

@if(session()->has('success'))
	<div class="alert alert-success alert-dismissible fade show" role="alert">
  	<strong>Success!</strong> {{ session()->get('success') }}
  	<button type="button" class="close" data-dismiss="alert" aria-label="Close">
    	<span aria-hidden="true">&times;</span>
  	</button>
	</div>
@endif

And by this way we can any flash messages for notify user. Either it's a success, error, warning or info.

So, we'll see the usage of flash messages in this chapter. And yes keep up to date with the github repository. That's it for today. See you in my next chapter. Happy coding 🙂.