我正试图从产品列表中添加一个产品到我的购物车中。我用的是拉拉维尔。在下面的代码中,我设法将单击order的每个产品添加到购物车中(这只是一个会话变量)。现在,我试图添加一个数量输入,以便为每个产品,我可以选择一个数量的产品,以添加到我的购物车。
请记住,从下面的代码中可以看出,Products是一个雄辩的集合,而在该集合中,我没有属性数量。在我看来,这也是不需要的,因为数量不是产品模型的一部分,而是属于我的购物车的东西。
下面是我的Livewire php部分。
class Dashboard extends Component
{
use WithPagination;
public string $search = '';
public function render(): View
{
return view('welcome', [
'products' => Product::search('description', $this->search)->paginate(9)
])->layout('layouts.app');
}
public function addToCart(int $productId)
{
Cart::add(Product::where('id', $productId)->first());
}
}这是刀片部分:
@foreach($products as $product)
<x-table.row wire:loading.class.delay="opacity-50" wire:key="{{ $product->id }}">
<x-table.cell>{{ $product->description }}</x-table.cell>
<x-table.cell> €{{ number_format($product->price, 2) }}</x-table.cell>
<x-table.cell>
<input type="number"name="quanity" class="w-16 mx-8" />
<button wire:click="addToCart({{$product->id}})">Order</button>
</x-table.cell>
</x-table.row>
@endforeach我认为最好的选择是向addToCart函数中添加第二个参数,即数量。但我做不到。
建议是非常欢迎的。
谢谢。
发布于 2022-02-15 03:15:41
像这样的东西可能就足够了
public function addToCart(int $productId, int $quantity)
{
Cart::add(Product::where('id', $productId)->first()->setAttribute('quantity', $quantity));
}https://stackoverflow.com/questions/71118375
复制相似问题