EmilKaminski's avatar

Laravel flash session does not persist when redirecting

Hello everyone, how are you? I'm using Laravel 11 + Vue.js + Inertia.js and facing the simple and strange issue related flash session. In Laravel controller, I am redirecting to other route and used 'with' method to send flash message. And tried to get flash session value in 'HandleInertiaRequests' middleware. But I can't get flash session value, it doesn't persist when redirecting. How to resolve the this issue?


Let me share code snippet

ListingController.php

return redirect()
            ->route('listing.index')
            ->with('success', 'Listing was created!');

HandleInertiaRequests.php

return array_merge(parent::share($request), [
            'flash' => [
                'success' => $request->session()->get('success')
            ]
        ]);
0 likes
10 replies
kodyxgen's avatar

Hi, could you try as in the documentation

'flash' =>  [ 
'success'  =>	fn() => $request->session()->get('success') 
]
EmilKaminski's avatar

@kodyxgen Thanks for your reply. Your way is new for me but it makes the same results. Also, when redirect to the other route, it must pass the controller and in the controller I was log the session status with Session::all(). But I couldn't see the any flash session data.

EmilKaminski's avatar

@Hassankhan Thanks for your answer for my question. But I am not using alert yet. Just was following the one new project code and I faced the this issue. It's is no need alert yet for me. I wanted to know why flash session crash when redirect and how to prevent it. Thank you!

mehdirth's avatar

Try to call directly the session like this: session('success')

EmilKaminski's avatar

@mehdirth Hello.. Thanks for your answer for my question. It is same results, please note that flash session data does not exist after redirect. I am waiting the answer who can help me why it's crash and how to resolve this issue. I tried to find solutions on google, but I can't find any helpful way for me. Thank you!

EmilKaminski's avatar

@mehdirth Yes, that is so weired, that is why I post this problem on this community.

Let me share my codes.

I'm redirecting with success flash session.

return redirect()
            ->route('listing.index')
            ->with('success', 'Listing was changed!');

In listing.index route handled by ListingController Index function.

public function index(Request $request)
    {
		Log::info('Flash session: ', [$request->session()->get('success')];
        return inertia(
            'Listing/Index',
            [
                'listings' => Listing::all()
            ]
        );
    }

Result: null , null

For share my whole code, it's a bit difficult, so I share with you main logic of my code now.

aruszala's avatar

@emilkaminski take a look here:

https://inertiajs.com/shared-data#flash-messages

Flash messages

Another great use-case for shared data is flash messages. These are messages stored in the session only for the next request. For example, it's common to set a flash message after completing a task and before redirecting to a different page.

Here's a simple way to implement flash messages in your Inertia applications. First, share the flash message on each request.

class HandleInertiaRequests extends Middleware
{
    public function share(Request $request)
    {
        return array_merge(parent::share($request), [
            'flash' => [
                'message' => fn () => $request->session()->get('message')
            ],
        ]);
    }
}

Next, display the flash message in a frontend component, such as the site layout.

<template>
  <main>
    <header></header>
    <article>
      <div v-if="$page.props.flash.message" class="alert">
        {{ $page.props.flash.message }}
      </div>
      <slot />
    </article>
    <footer></footer>
  </main>
</template>

I hope this helps.

1 like
EmilKaminski's avatar

@aruszala Thanks for your answer for my question. I was following the guide what actually same you shared with me. Video tutorial working fine related this flash session, but for me, it's not working.

Additionally, when we submit the form from vue and I validate it from laravel. and if it's invalid, it has to have the error flash session, but it also not working.

Please or to participate in this conversation.