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

murilo's avatar
Level 10

Laravel 8 , session in api is not working

hello , I have a shopping cart . I am working with VUE js in front , and storing this shopping cart in Laravel ( backend ) with session . Probably this is not the best way , but I have this code and it as working before . In laravel 6, I had to make some modification to make the session work with API in laravel , this modification in knew -

 'api' => [
            \Illuminate\Session\Middleware\StartSession::class,
....

Like this example - https://stackoverflow.com/questions/47878898/session-not-working-in-laravel-api

And my session -

// SAVE SESSION
  $request->session()->put('cart' , $cart);
  $request->session()->save();

// GET THE SESSION
 $cart = $request->session()->get('cart');
dd($cart)

This code was working in Laravel 6 . but in Laravel 8 , is not working the session in API any more BUT it works in web routes ( outside of API ).

0 likes
6 replies
Snapey's avatar

if you want sessions just put your routes in web.php

UsmanBasharmal's avatar

some other middlewares could be interfering, try to disable them systematically

PHPLaraDev's avatar

Are you have the solution !!! the same error here session don't work on api route ? i made route middleware also the same thing ???

Snapey's avatar

@LaraCoder7 routes in api.php are STATELESS. (no sessions). Put your route in web.php if you want sessions

Please or to participate in this conversation.