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

msislam's avatar

Error : "strlen(): Argument #1 ($string) must be of type string, array given" with anayarojo/shoppingcart

Dear Sir, I am new in programing and started a Laravel Inertia Ecommerce project. I am trying to add a shopping cart from anayarojo/shoppingcart. But it showing the following error : strlen(): Argument #1 ($string) must be of type string, array given

My controller is as bellow:

public function index(CartService $cartService) { return Inertia::render('Cart/Index', [ 'cartItems' => $cartService->setCartValues()->get('cartItems'), 'cartTaxRate' => $cartService->setCartValues()->get('cartTaxRate'), 'cartSubtotal' => $cartService->setCartValues()->get('cartSubtotal'), 'code' => $cartService->setCartValues()->get('code'), 'discount' => $cartService->setCartValues()->get('discount'), 'newTax' => $cartService->setCartValues()->get('newTax'), 'newSubtotal' => $cartService->setCartValues()->get('newSubtotal'), 'newTotal' => $cartService->setCartValues()->get('newTotal'), 'laterItems' => $cartService->setCartValues()->get('laterItems'), 'laterCount' => $cartService->setCartValues()->get('laterCount'), ]); }

   public function store(Request $request)
{
    Cart::instance('default')->add(
        $request->id,
        $request->name,
        $request->quantity,
        $request->price,
        [
            'totalQty' => $request->totalQty,
            'product_code' => $request->product_code,
            'image' => $request->image,
            'slug' => $request->slug,
            'details' => $request->details
        ]
    )->associate('App\Models\Product');

    return redirect()->route('cart.index');
  }

And my vue template is as follows : {{ $page.props.cartCount }} item(s) in cart

How can I solve the problem?

0 likes
3 replies
Snapey's avatar

please format your question correctly

1 like
anayarojo's avatar

Hi @msislam

You forgot the $weight parameter, please change that

from

Cart::instance('default')->add(
        $request->id,
        $request->name,
        $request->quantity,
        $request->price,
        [
            'totalQty' => $request->totalQty,
            'product_code' => $request->product_code,
            'image' => $request->image,
            'slug' => $request->slug,
            'details' => $request->details
        ]
    )->associate('App\Models\Product');

to


$weight = 0;

Cart::instance('default')->add(
        $request->id,
        $request->name,
        $request->quantity,
        $request->price,
        $weight,
        [
            'totalQty' => $request->totalQty,
            'product_code' => $request->product_code,
            'image' => $request->image,
            'slug' => $request->slug,
            'details' => $request->details
        ]
    )->associate('App\Models\Product');

Regards

1 like
msislam's avatar

@anayarojo Thanks a lot. After taking actions as per your direction the problem has been eliminated. But the value of $cartCount on icon is not showing.

Please or to participate in this conversation.