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

Chrizzmeister's avatar

Show session for specific user

Hi, this is a theoretical question because i am really struggling with the logic of this. I am building a webshop and i have a shopping Cart, everything is working fine, i can add/show/delete items in my cart. using this package: https://github.com/Crinsane/LaravelShoppingcart

But the items in my cart are not stored in my database. So i assume they are stored in a session. What i want to do is show my shopping cart only for the user who puts them in the cart. (I already have middleware setup so only authenticated users can put or see items in the cart) When using my database i make a relation and i just do Auth::user()->Cart(). But i'm not sure how to tackle it without relationships ? Do i make a model of my shopping cart even though i have no table for it?

edit:

this is my addToCart logic, how should i say i want to assing this to a session from the current user? , because the package probably has some logic in it that stores it in a session.

public function addItem(WinkelMandRequest $request, $id)
    {

        $product =   Product::findOrFail($id);
        $aantal  =   $request->input('aantal');
        $formaat =   $request->input('formaat_list');
        $kleur   =   $request->input('kleur_list');

        Cart::add(array(

            'id'        => $product->id,
            'name'      => $product->naam,
            'qty'       => $aantal,
            'price'     => $product->prijs,
            'options'   => array('formaat' => $formaat, 'kleur' => $kleur, 'foto' => $product->foto)

            ));

        return redirect('winkelmand');
    }
0 likes
6 replies
toniperic's avatar

As I could see from the API of that package, you can do Cart::count() which will return the number of items in the cart. So just show the cart if there are more than 0 items in the cart.

Hope that helps.

Chrizzmeister's avatar

@toniperic that 's not really my question, I have used Cart::count() to show the items in my cart. I have used all the basic functionalities of that package . I want to show a personal cart for every user. (like a relation) . for example : user 1 orders product A , Shopping cart shows product A, but when user 2 logs in , he shouldnt see a shopping cart containing product A, he should see an empty card because he didn't add anything

toniperic's avatar

First things first - how can user2 see the product A in the cart if user1 has added that to the cart? This shouldn't be possible, especially if the contents of the cart are stored in each user's session.

Chrizzmeister's avatar

@toniperic ok that seems logical , this is my addToCart logic, how should i say i want to assing this to a session from the current user? , because the package probably has some logic in it that stores it in a session.

public function addItem(WinkelMandRequest $request, $id)
    {

        $product =   Product::findOrFail($id);
        $aantal  =   $request->input('aantal');
        $formaat =   $request->input('formaat_list');
        $kleur   =   $request->input('kleur_list');

        Cart::add(array(

            'id'        => $product->id,
            'name'      => $product->naam,
            'qty'       => $aantal,
            'price'     => $product->prijs,
            'options'   => array('formaat' => $formaat, 'kleur' => $kleur, 'foto' => $product->foto)

            ));

        return redirect('winkelmand');
    }
shoki's avatar

I think maybe if you pass DB::table('users') in index for WelcomeControler or '/' looking for a login user Auth::user()->get(); 'id' of course .... with that you can catch in index and work with user.

Chrizzmeister's avatar

@shoki, working with my user is no problem, you can catch it's id everywhere with Auth::user()->id , but thats not my question

Please or to participate in this conversation.