laravel 添加产品按钮没有响应

mfuanj7w  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(129)

我正在学习使用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,
        )
    ]);

}

问题是+按钮没有响应,为什么不工作?

3z6pesqy

3z6pesqy1#

你应该保持循环中的组件跟踪,并且你应该传递$post insead of $post-〉id:

@livewire('products',['product'=> $post], key($user->id))

产品刀片:

<button  wire:click="addToCart()">+</button>

在产品控制器中:

class Products extends Component
{
   public $product;

   public function addToCart()
   {
       Cart::add([
        'id' => $this->product->id,
        'name' =>  $this->product->name,
        'price' =>  $this->product->price,
        'quantity' =>  $this->product->quantity,
        'attributes' => array(
            'image' =>  $this->product->image,
        )
       ]);
   }
}

相关问题