Flash Messages in Laravel
Flash Messages in Laravel
Flash messages in Laravel are used to store a message in the session that will only be available for the next HTTP request. This feature is particularly useful for showing success, error, or warning messages after performing actions like form submissions, data updates, or deletions.
Flash messages can be used for scenarios like:
- Showing a confirmation message after a user submits a form.
- Displaying an error message if an operation fails.
- Flashing notifications when a user logs in or out.
How Flash Messages Work
Flash messages are stored in the session temporarily and will be automatically removed after they are displayed on the next request. This ensures that they don’t persist beyond the page where they are needed.
Setting Flash Messages
To set a flash message, you use the flash()
method provided by Laravel’s session system. Here’s how you can store a flash message:
Example: Setting a Flash Message
public function store(Request $request)
{
// Store a success message in the session
session()->flash('success', 'Data has been saved successfully!');
return redirect()->route('home');
}
In this example, the flash()
method stores a message with a key (success
) and a value (Data has been saved successfully!
). This message will only be available on the next request, which means it will be available after the user is redirected.
Displaying Flash Messages in Views
To display the flash message, you can check for its presence in the session and output it in your Blade views:
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
Here, we are checking if the session has a flash message with the key success
and displaying it inside a Bootstrap-styled alert box. If the flash message exists, it will be shown; otherwise, nothing will be displayed.
Flashing Multiple Messages
You can flash multiple messages to the session by calling the flash()
method multiple times or by passing an array of messages.
Example: Flashing Multiple Messages
public function store(Request $request)
{
// Store multiple flash messages in the session
session()->flash('success', 'Data has been saved successfully!');
session()->flash('warning', 'Please review your profile details.');
return redirect()->route('home');
}
In the view:
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if(session('warning'))
<div class="alert alert-warning">
{{ session('warning') }}
</div>
@endif
Flashing Input Data
Laravel also provides a method to flash the old input data back to the session. This is useful when you want to repopulate form fields after validation errors, so users don't have to re-enter their input.
Example: Flashing Old Input
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email',
]);
// If validation fails, redirect back with the old input
return redirect()->back()->withInput();
}
In the Blade view, you can use the old()
helper to repopulate the form fields with the previously submitted data:
<input type="text" name="name" value="{{ old('name') }}" />
<input type="email" name="email" value="{{ old('email') }}" />
Flashing Data for Multiple Requests
By default, flash data is only available for the next request. If you need the flash data to persist for multiple requests, you can use the reflash()
method or the keep()
method.
Reflash: This method will keep all the flashed data for the next request.
session()->reflash();
Keep: This method allows you to keep specific flashed data for the next request.
session()->keep(['success']);
Example: Complete Flash Message Workflow
Here’s a simple example of how flash messages work in a typical Laravel application:
// Controller method for storing data
public function store(Request $request)
{
// Validate the incoming request
$request->validate([
'title' => 'required',
'content' => 'required',
]);
// Perform the action (e.g., save data)
Post::create($request->all());
// Flash a success message
session()->flash('success', 'Post created successfully!');
// Redirect to another page (e.g., home)
return redirect()->route('home');
}
// Blade view to display the flash message
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif