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

ajithlal's avatar

How to pass array as second parameter in livewire function

Hi All, I have a livewire component for my cart functionality. In that I have to pass an array as second parameter to increment function. how can I achieve that?

Cart component

class Cart extends Component
{
    public $cart;
    public $item;

    public function mount(array $item): void
    {
        $this->cart = CartFacade::get();
        $this->item = $item;
    }

    public function render()
    {
        return view('livewire.cart');
    }

    public function increment(int $productId, array $item)
    {
       .....
    }
}

cart blade

<tr class="cart_item">
    ....
    <td class="product-quantity" data-title="Quantity">
        <div class="detail-qty info-qty border radius6 text-center">
            ....
            <span class="qty-val">{{ $item['cart_count'] }}</span>

            <a href="#" class="qty-up" wire:click="increment({{ $item['product_id'] }},'{{ $item }}')"><i
                    class="fa fa-angle-up" aria-hidden="true"></i></a>
        </div>
    </td>
   ....
</tr>

This is my code. It returning an error htmlspecialchars() because of the second parameter is an array.

0 likes
10 replies
bugsysha's avatar

Remove quotes.

<a href="#" class="qty-up" wire:click="increment({{ $item['product_id'] }},{{ $item }})">
bugsysha's avatar

What about

<a href="#" class="qty-up" wire:click="increment({{ $item['product_id'] }}, @json($item))">
ajithlal's avatar

returning this error. Unable to call component method. Public method [increment(2,{] not found on component: [cart]

deansatch's avatar

It JS, you need to implode it then explode within your controller

1 like
ajithlal's avatar

I tried json_encode() and it worked. but not sure it is a better way or not.

Snapey's avatar

why do you need to send the array of item when it is already a property of your component?

ajithlal's avatar

I want to update the item count in the cart. I tried with $this->item['cart_count'] +=1 and it updates but the result is wrong. Means, if we have two items in the cart and we increment the count of one item then, count of two cart items are updating. Also when I remove the item from the cart, it is not reflecting immediately. it reflecting only after refreshing the page.

Snapey's avatar
Snapey
Best Answer
Level 122

you need to examine the structure of $item, not pointlessly sending it from the view

ajithlal's avatar

Thank you. It was some other errors. A js files was doing the count increment functionality.

Please or to participate in this conversation.