Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel
community creator

How to flash data to the session in Laravel

Chinweuba Elijah Azubuike

Overview

Have you ever wanted to display a message or store items once in your application?

While building our application we see reasons to do this, whether its displaying welcome to an authenticated user or showing a successful message after an action.

Laravel can store such messages or data in your session, and they will only be available during the successive HTTP request. It gets deleted afterwards. We use the flash() method in this manner:

$request->session()->flash('message', 'Action was Successful');

Let see how to flash data during a redirect.

Redirecting from a page and flashing data to the session are done at the same time. This is used for saving data after an action:

Route::post('admin/profile', function () {
    // Update the user's profile...

    return redirect('dash')->with('message', 'Successfully Updated!');
});

Summary

Flashing data to the session stores short-lived data and can display it in the application. It is usually used to display status after an action.

RELATED TAGS

laravel
community creator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring