Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Annaro's avatar

Session variable not working

Here is my form:

<form method="POST" action="{{ route('item.deactivate', [ 'item' => $item->id ]) }}">
            @csrf
            <button type="submit">Deactivate</button>
            <input type='submit' value="Deactivate" onclick="return confirm('Deactivate?')"/>
</form>

As you can see, i included 2 versions of a Deactivate button.

First button does not show a confirmation pop-up, but it does show a confirmation message AFTER deactivation of the item. Second button is supposed to do exactly the same, aside from the fact that it does show a confirmation pop-up. BUT i don't understand why, when i use this second button, it doesn't display a confirmation message AFTER.

Whether i click on button 1 or button 2, the item is actually deactivated, so the main part works. But i am curious to know why i can't show a session message with the second button...

Here is the function in my controller:

    public function deactivate(Item $item)
    {
        if($item->user_id == auth()->user()->id)
        {
            $item->update([
                'active' => 0
            ]);
            return redirect()->back()->with('message', 'Item deactivated!');
        }
        else
        {
            abort(403);
        }
    }
<?php var_dump(session()->all()); ?>

var_dump when i use the first button: array(6) { ["_token"]=> string(40) "NkADD5mgWRG9AJFofDmtJGQfSExUyk7mcmBODpWO" ["_flash"]=> array(2) { ["old"]=> array(1) { [0]=> string(7) "message" } ["new"]=> array(0) { } } ["_previous"]=> array(1) { ["url"]=> string(29) "http://127.0.0.1:8000/account" } ["login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d"]=> int(1) ["url"]=> array(1) { ["intended"]=> string(45) "http://127.0.0.1:8000/virtual_assistant_tools" } ["message"]=> string(19) "Item deactivated!" }

var_dump when i use the second button: array(5) { ["_token"]=> string(40) "NkADD5mgWRG9AJFofDmtJGQfSExUyk7mcmBODpWO" ["_flash"]=> array(2) { ["old"]=> array(0) { } ["new"]=> array(0) { } } ["_previous"]=> array(1) { ["url"]=> string(29) "http://127.0.0.1:8000/account" } ["login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d"]=> int(1) ["url"]=> array(1) { ["intended"]=> string(45) "http://127.0.0.1:8000/virtual_assistant_tools" } }

0 likes
4 replies
Annaro's avatar
Annaro
OP
Best Answer
Level 2

@Braunson Oh yes, it definitely gets inside the if statement. For some reason though (and i noticed the same behavior with other redirects), it seems like the message is not passed with the redirect. I solved it using:

session()->put('message', 'Item deactivated!'); 
session()->save();
return redirect()->back();

instead of doing a redirect with the message.

Braunson's avatar

@Annaro Very weird, and this route you POST to is inside the web middleware (and only once, check by running php artisan route:list and looking at the route in question's middleware applied to it)? You are using a recent 8.x/9.x version of Laravel and are not using this with Livewire?

Since I was able to re-create it in a sandbox, I believe it may be related to something other then the code posted, likely middleware (as mentioned), possibly the session driver used, or something else.

Glad to hear you figured a workaround thought.

1 like
Annaro's avatar

@Braunson Yes, the route is in the web.php file, it appears just once with php artisan route:list, and i am using Laravel 8. I do have Livewire components in the same view but nothing to do with this form. It definitely might have something to do with the session driver i guess. Thank you for helping :)

1 like

Please or to participate in this conversation.