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

Bastet's avatar

Destroying only the login session

I've created a site that uses laravel auth and has multiple sessions that happen after the user logs in. I've also managed to save the sessions into the database. What I'm trying to figure out is how do I log out with out touching the other sessions.

0 likes
8 replies
bobbybouwmann's avatar

In general you keep one session for the user and destroy the whole session when they logout. If you want to keep data available you simply set a cookie instead. The session will not work if the user is logged out, because it's bound to that user. So cookie is the way to go ;)

Bastet's avatar

@BOBBYBOUWMANN - I'm still busy looking into it, I'm still having issues trying to understand it and get the cookies to work.

So far I've done this

public function getAddToCart(Request $request, $id)
    {


        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
        $contacts = Contact::all();

        $product = Product::find($id);

        $supplier_code = $request->supplier_code;

        $oldCart = Session::has('cart') ? Session::get('cart') : null;

        $value = "this is getAddToCart Cookie";

        $cart = new Cart($oldCart);
        $cart->add($product, $product->id, $supplier_code);

        $request->session()->put('cart', $cart);

        return ['redirect' => route('product.shoppingCart', ['products' => $cart->items, 'totalPrice' => $cart->totalPrice, 'menus_child' => $menus_child, 'contacts' => $contacts, 'supplier_code' => $supplier_code])->withCookie(Cookie::make('cookieTest', $value))];
    }

But I ended up with this error in my network tab in dev tools

Call to a member function withCookie() on string
bobbybouwmann's avatar

First of all you return a string for the route here and not a response object. Try this

$routeData = [
    'products' => $cart->items, 
    'totalPrice' => $cart->totalPrice, 
    'menus_child' => $menus_child, 
    'contacts' => $contacts, 
    'supplier_code' => $supplier_code,
];

return response()->route('product.shoppingCart', $routeData)->withCookie(Cookie::make('cookieTest', $value));
Bastet's avatar

@BOBBYBOUWMANN - I ended up getting this error

Method Illuminate\Routing\ResponseFactory::route does not exist.

Though would

return response()->route('product.shoppingCart', $routeData)->withCookie(Cookie::make('cookieTest', $value));

work even though I'm using vue.js?

Bastet's avatar

@BOBBYBOUWMANN - I think I'm almost there. I had to change

return response()->route('product.shoppingCart', $routeData)->withCookie(Cookie::make('cookieTest', $value));

to

return response(route('product.shoppingCart', $routeData))->withCookie(Cookie::make('cookieTest', $value));

but now when I go to my cart I just get a blank page and my url looks like this

http://shopping-site.test/product/undefined
Bastet's avatar

I'm not sure if I need to make a new question for this or if I can post it on here, but I almost have my cookies working perfectly, but I need to know if there is a way to save the cookies.

So what is happening now is that after I've made a cookie, when I log out I still have that cookie showing and what I would like is that when I log out the cookie is gone but when I log in the cookie is there.

Is that possible? And is it possible to make a cookie unique to a logged in user?

Please or to participate in this conversation.