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

Sharim's avatar

Collect All Instances of a Cart in Laravel

I've created 2 instances in my cart. But when I try to count or make subtotal of all the instances, it is only showing me the items of 1 instance. I'm using

Gloudemans\Shoppingcart

My Controller is:

$allinstances = collect([Cart::instance('default')->content(), Cart::instance('specials')->content()]);

$contents = Cart::content()->map(function ($item) {
            return $item->model->slug.', '.$item->qty;
        })->values()->toJson();
0 likes
1 reply
BenSeDev's avatar

First glance makes me thing you should map over your $allinstances variable, instead of Cart::content().

So you're making a collection of carts and putting them inside $allinstances;

$allinstances = collect([Cart::instance('default')->content(), Cart::instance('specials')->content()]);

But then you map over Cart::content(), where I think you should map over $allinstances. Something like this:

$contents = $allinstances->map(function ($item) {
            return $item->model->slug.', '.$item->qty;
        })->values()->toJson();

I hope this helps.

Please or to participate in this conversation.