 
                                     
                                        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.
– 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
public function index()
    {
        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 2:
Using the session()->flash() method.
We can also use session()→flash() to show the flash messages. Let's look at the below source code:
public function index()
    {
        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.
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">×</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.
– Happy coding 🙂.
All Comments