fahim152
fahim152

Reputation: 2629

Laravel Livewire key() expects parameter 1 to be array, integer given | nested components | loading a component inside a loop

I've been working with Laravel livewire for a while now, I've a nested components, which is product list for my site and inside that list I've another component for adding product to wishlist. As per documentation stated here , it says

"Similar to VueJs, if you render a component inside a loop, Livewire has no way of keeping track of which one is which. To remedy this, livewire offers a special "key" syntax:"

Like this:

<div>
    @foreach ($users as $user)
        @livewire('user-profile', $user, key($user->id))
    @endforeach
</div>

Here is my code snippets from my project.

<div>
    @foreach($products as $product)
        <div class="product-box white-bg mb-8" data-dusk="product"> 
             {{-- here im passing product id as param in key(),  'productList' is a static value for a variable of mount(). --}}
                @livewire('desktop.wish-list-add', $product, key($product->id), 'productList')

            <div class="product-content d-flex justify-content-between align-items-center p-5">
                ............... 

    @endforeach
    {{ $products->links() }}
</div>

The issue is when I try to pass $product->id as param for key(), it gives error

key() expects parameter 1 to be array, integer given

But the doc clearly shows that we have to pass id as param. Has anyone faced this issue so far?

Upvotes: 3

Views: 5244

Answers (2)

user10755185
user10755185

Reputation: 41

@livewire('photos.photo-wire', ['photo' => $photo], key($photo->id))

instead of

@livewire('photos.photo-wire', ['photo' => $photo, key($photo->id)])

because that will generate the error:

key() expects parameter 1 to be array, integer given

Upvotes: 4

fahim152
fahim152

Reputation: 2629

okay, I found the solution ( however it doesn't make sense to me , but it works :/ ) You have to pass other parameters for mount() like this:

@livewire('desktop.wish-list-add', 'productList', $product->id, key($product->id))

Instead of this:

@livewire('desktop.wish-list-add', $product, key($product->id), 'productList')

Upvotes: 2

Related Questions