Laravel Livewire key() 期望参数 1 为数组,整数给定 |嵌套组件 |在循环中加载组件
Laravel Livewire key() expects parameter 1 to be array, integer given | nested components | loading a component inside a loop
我一直在使用 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 ,它说
"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" 语法:"
像这样:
<div>
@foreach ($users as $user)
@livewire('user-profile', $user, key($user->id))
@endforeach
</div>
这是我项目中的代码片段。
<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>
问题是当我尝试将 $product->id 作为参数传递给 key() 时,出现错误
key() expects parameter 1 to be array, integer given
但是文档清楚地表明我们必须将 id 作为参数传递。到目前为止有没有人遇到过这个问题?
好的,我找到了解决方案(但它对我来说没有意义,但它有效:/)
您必须像这样为 mount() 传递其他参数:
@livewire('desktop.wish-list-add', 'productList', $product->id, key($product->id))
而不是这个:
@livewire('desktop.wish-list-add', $product, key($product->id), 'productList')
@livewire('photos.photo-wire', ['photo' => $photo], key($photo->id))
而不是
@livewire('photos.photo-wire', ['photo' => $photo, key($photo->id)])
因为这会产生错误:
key() expects parameter 1 to be array, integer given
我一直在使用 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 ,它说
"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" 语法:"
像这样:
<div>
@foreach ($users as $user)
@livewire('user-profile', $user, key($user->id))
@endforeach
</div>
这是我项目中的代码片段。
<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>
问题是当我尝试将 $product->id 作为参数传递给 key() 时,出现错误
key() expects parameter 1 to be array, integer given
但是文档清楚地表明我们必须将 id 作为参数传递。到目前为止有没有人遇到过这个问题?
好的,我找到了解决方案(但它对我来说没有意义,但它有效:/) 您必须像这样为 mount() 传递其他参数:
@livewire('desktop.wish-list-add', 'productList', $product->id, key($product->id))
而不是这个:
@livewire('desktop.wish-list-add', $product, key($product->id), 'productList')
@livewire('photos.photo-wire', ['photo' => $photo], key($photo->id))
而不是
@livewire('photos.photo-wire', ['photo' => $photo, key($photo->id)])
因为这会产生错误:
key() expects parameter 1 to be array, integer given