Flashdata
Flashdata survives exactly one additional request. It is the standard way to show a success or error message after a redirect.
Set a flash message
return redirect()->to('/products')->with('success', 'Product created.');
with() writes the value as flashdata before redirecting.
Read and display
if (session()->has('success')) {
echo '<div class="alert alert-success">' . esc(session('success')) . '</div>';
}
Using the session directly
$session = session();
$session->setFlashdata('success', 'Saved.');
$message = $session->getFlashdata('success');
Keep it for another request
$session->keepFlashdata('success');
Use keepFlashdata() only when a second request should also display the message, for example after a redirect chain.
Good practices
- Store short, plain messages; do not put large objects or sensitive data in flashdata.
- Escape the message when rendering it.
- Display messages in one shared layout partial so every page behaves the same.
- Use flashdata for outcomes, not for form values; old input has its own mechanism.