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

Kris01's avatar

Laravel Session after redirect

Hello Guys,

So I am having this problem where if I do

return redirect()->back()->with(['data'=>'error']);

I can access the session just fine, but if I do

return redirect()->route('route.name')->with(['data'=>'error']);

The session won't exists after the redirect, so I am not able to access it, it's null.

Edit:: Maybe the fact that I am doing this in the hadle method in the middelware has something to do with this?

0 likes
15 replies
tykus's avatar

Has the Session started before this middleware is handled?

Kris01's avatar

@tykus It's the 'app.user' one

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'app.guest' => \App\Http\Middleware\AppGuest::class,
        'app.user' => \App\Http\Middleware\AppUser::class,
        'app.seller' => \App\Http\Middleware\AppSeller::class,
        'app.buyer' => \App\Http\Middleware\AppBuyer::class,
        'app.admin' => \App\Http\Middleware\AppAdmin::class
    ];
tykus's avatar

@Kris01 that should be well after the Session is started - I guess (based on the name of the middleware) that you have an authenticated user that you are working with in this middleware method? Can you share the code in the middleware?

Kris01's avatar

@tykus Basically, one of the things I am doing, is checking his percentage of profile completition, if it is below 90% he should be redirected to his profile with a message.

 if(Auth::user()->account_type == 'buyer' && profile_progress() < 92){
            return Redirect::route('app.profile')->with(['toasts_error'=>'complete your profile 100%']);
        }
tykus's avatar
tykus
Best Answer
Level 104

@Kris01 does the 'app.profile' route also use this route middleware; or another middleware that overwrites the Session?

Kris01's avatar

@Snapey still the same. Probably is getting overwritten, as @tykus says, but how can I solve this problem then?

tykus's avatar

@Kris01 but if it has the same middleware, then there would be potentially an infinite redirect (eventually you will see ERR_TOO_MANY_REDIRECTS message) - depending on that other middleware.

You seem to have a lot going in in these middlewares; are you able to reason about the outcomes for the various different scenarios???

Kris01's avatar

@tykus Ok so, nevermind, thanks to your answer above I did this in my handle method

        return session()->has('toasts_error') ? $next($request)->with('toasts_error', session()->get('toasts_error')) : $next($request);

And not it works

tykus's avatar

@Kris01 I don't know what's going on here TBH... if the Session has the toasts_error why would it be necessary to set it again?

Snapey's avatar

@Kris01 flashed data only lives for one request cycle, so if there is a second redirect for some reason then the data will be lost unless it is re-flashed.

1 like
Kris01's avatar

@Snapey @tykus hey guys, thanks for the help. @tykus I'm re-saving it because, as @snapey says, the session is saved only for the next request. It's going two times through the middleware so I have to re-save it

Please or to participate in this conversation.