Laravel Session Flash Example

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023


In this tutorial we will learn how to use Laravel session flash.


How to Laravel Session Flash

In Laravel, the Session class provides a simple way to store data for the duration of a user's session. One of the useful features of the Session class is the ability to flash data to the session for a single request. This means that the data will be available only until the next request is made, and will then be deleted from the session.

Here is an example of how to use the Session class to flash data in Laravel:

public function store(Request $request)
    // Store the data in the database

    // Flash a success message to the session
    session()->flash('success', 'Data has been saved successfully.');

    // Redirect the user back to the form
    return redirect()->back();


In the example above, we are using the session() helper function to flash a success message to the session. We pass two arguments to the flash method: the key ('success') and the value ('Data has been saved successfully.').

Once the data has been flashed to the session, we redirect the user back to the form using the redirect() helper function and the back() method.

To display the flashed data in a view, you can use the session() helper function again with the get method, like so:

@if (session()->has('success'))
    <div class="alert alert-success">
        {{ session()->get('success') }}


In this example, we are checking if the success key exists in the session using the has method. If it does exist, we display the flashed data in a success message using the get method.

That's it! You should now be able to use the Session class to flash data in Laravel.


Comments 0

Leave a comment