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

wfajriansyahh's avatar

Session Flash not Working

Hello, i want to ask something.

Anyone know why my session flash not working ? i have to try for session:put() and working fine.

app\Http\Kernel.php

protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\LocalizationMiddleware::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
];

** Edited: I use this for set session flash:

function checkVariant() {
...
   if($this->variant != $variant_total) {
       session()->flash("error", 'Variant need to filled.');
       return redirect()->to('/product/'.$this->product->slug);
   }
}

Sorry for my bad english.

0 likes
22 replies
sr57's avatar

Try

Session::flash("error", 'Variant need to filled.');
sr57's avatar

@wfajriansyahh

! ?

try this

return redirect()->to('/product/'.$this->product->slug)->with("error", 'Variant need to filled.');
ollie_123's avatar

@wfajriansyahh are you including your error on the page?

I have a file of flash messages i use that i just include in the head above $slot.

eg,

@if (session()->has('message'))
    <div class="bg-green-500 flash-message">
        <div class="text-white text-sm"> {{ session('message') }}</div>
    </div>
@endif	

@if (session()->has('error')).....
wfajriansyahh's avatar

@ollie_123 Yes, i add that btw.

<div class="col-12 col-lg-8">
                    @if(Session::has('error'))
                    <div class="alert alert-danger">{{ Session::get('error') }}</div>
                    @endif
...
tykus's avatar

There is more that one redirect perhaps?

wfajriansyahh's avatar

@tykus There only one redirect. That function only checking for variants. If not required/not pass the if case. we will redirected to page product.

This problem not on the product page only, i have authentication. then cant get session flash for alerting wrong password or etc. :(

ollie_123's avatar

@wfajriansyahh Are you using $rules & validation?

If so put this under your input field...

@error('your_field_name') <span>*{{ $message }}</span> @enderror
ollie_123's avatar

@wfajriansyahh couple of things:-

are you using Livewire? Have you done a dd() test inside of the if condition to check that its working inside?

You could try adding the session onto the redirect:-

return redirect()->route('/product/' . $this->product->slug)->with("error","Not valid");
wfajriansyahh's avatar

@ollie_123 Yes, i use the livewire.

Also, i have been try on new project + livewire. and i test with session flash. its working.

ollie_123's avatar

@wfajriansyahh you could try adding a custom error instead of flashing one to the session,

try somthing like this...

public $variantError = NULL;

function checkVariant() 
{
    if($this->variant != $variant_total) 
    {
        $this->addError('error', 'Variant Error');
        $this->variantError = 'Variant Error';
    }
    else
    {
        // Your code here
    }
}

Then in blade...

<input.....>
 @if($variantError)
		<span class="text-red-500 text-xs italic sm:mb-0 block pb-1">*{{ $promoError }}</span>
@endif
ollie_123's avatar

But session flash will only flash once until the page is refreshed so you may be better adding a custom error.

sr57's avatar

@wfajriansyahh

Sorry no more idea, except debugging inside the framework to find when/where the session variable is reseted

Please or to participate in this conversation.