Laravel 12 Flash Message Functionality Integration with Example
In Laravel 12, implementing flash messages is straightforward thanks to its session handling capabilities. Flash message is a great way to provide users with temporary feedback after performing actions like form submissions, updates, or deletions in a web application. In this article , we'll try to cover how to add flash message functionality into our Laravel project effectively.
What is Flash Message?
Flash message is short-lived notification displayed to users after a specific action, typically persisting for only one page load or redirect. This is perfect for confirming successful operations (e.g., "Record saved!") or alerting users to errors (e.g., "Something went wrong."). Laravel 12 makes this feature easy to implement using its session system.
Step 1: Setting Up the Controller Logic
To start, let’s add the flash message logic in our controller. Here's an example of how we might handle this in a UserController. In this example, after validating and processing the request, we call session()->flash() to store a temporary message under the key 'success'. This message will be available only for the next request.
namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function store(Request $request) { // Validate and store the user data $request->validate([ 'name' => 'required', 'email' => 'required|email', ]); // Simulate saving the user (replace with actual logic) // User::create($request->all()); // Set a success flash message session()->flash('success', 'User successfully created!'); return redirect()->route('users.index'); } }
Step 2: Displaying Flash Messages in the View
Next, we'll need to show the flash message in our Blade template. A common approach is to create a reusable partial that checks for flash messages and displays them. Here’s how we can do it:
Create a partial file, e.g., resources/views/partials/flash.blade.php:
@if (session('success')) <div class="alert alert-success"> {{ session('success') }} </div> @endif @if (session('error')) <div class="alert alert-danger"> {{ session('error') }} </div> @endif
Include this partial in your main layout file (e.g., resources/views/layouts/app.blade.php):
<!DOCTYPE html> <html> <head> <title>My Laravel App</title> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <div class="container"> @include('partials.flash') @yield('content') </div> </body> </html>
Step 3: Adding Error Handling
We might also want to display error messages when something fails. It needs to modify the controller to include an error case:
public function store(Request $request) { try { $request->validate([ 'name' => 'required', 'email' => 'required|email', ]); // Simulate a failure scenario if (rand(0, 1)) { throw new \Exception('Random failure!'); } session()->flash('success', 'User created successfully!'); } catch (\Exception $e) { session()->flash('error', 'Failed to create user: ' . $e->getMessage()); } return redirect()->route('users.index'); }
Hope that, this article have given you a clear idea how to implement flash message in laravel 12 application