我正在学习使用livewire,我正在做一个网上商店。按钮**+**添加产品不起作用
单击+应添加产品
<div wire:ignore.self>
<div style=" box-shadow: 0 0px 0.9px rgb(0 0 0 / 7%), 0 0px 7px rgb(0 0 0 / 14%);"
class="list-unstyled w-100 rounded d-flex align-content-center">
@forelse ($results as $post)
<div class="w-25"><img style="height: 100px; width: 100px; objet-fit:cover;" class="" src="{{ $post->imagen}}"
alt=""></div>
<div class="w-70 mr-4 text-left pl-4 pt-1">
<li style=" font-size: 17px;
font-weight: 600;
background-color: white;
color: #4c4c4c;"> {{ $post->nombre }}</li>
<li> $ {{ $post->precio }}</li>
</div>
@livewire('products',['productoid'=>$post->id])
@empty
<li>No hay resultados para "{{ $search }}"</li>
@endforelse
</div>
El componente que utilizo se llamaproducts
<div>
<button wire:click="addToCart({{$productoid}})">+</button>
<div>
控制器:
public function addToCart(Request $request)
{
dd($request);
Cart::add([
'id' => $request->id,
'name' => $request->name,
'price' => $request->price,
'quantity' => $request->quantity,
'attributes' => array(
'image' => $request->image,
)
]);
}
问题是+按钮没有响应,为什么不工作?
1条答案
按热度按时间3z6pesqy1#
你应该保持循环中的组件跟踪,并且你应该传递$post insead of $post-〉id:
产品刀片:
在产品控制器中: