嘿伙计们,我的按钮有问题,它没有执行我想要的功能,也没有给我任何错误,所以我不知道会发生什么 这是我的一些购物车控制器代码
$products = Product::orderBy('created_at', 'DESC')->get();
$condition = new \Darryldecode\Cart\CartCondition([
'name' => 'pajak',
'type' => 'tax',
'target' => 'total',
'value' => $this->tax,
'order' => 1
]);
\Cart::session(Auth()->id())->condition($condition);
$items = \Cart::session(Auth()->id())->getContent()->sortBy(function ($cart){
return $cart->attributes->get('added_at');
});
if(\Cart::isEmpty()){
$cartData = [];
}else{
foreach($items as $item){
$cart[] = [
'rowId' => $row->id,
'name' => $item->name,
'pricesingle' => $item->harga,
'price' => $item->getPriceSum(),
];
}
$cartData = collect($cart);
}
$sub_total = \Cart::session(Auth()->id())->getSubTotal();
$total = \Cart::session(Auth()->id())->getTotal();
$newCondition = \Cart::session(Auth()->id())->getCondition('pajak');
$pajak = $newCondition->getCalculatedValue($sub_total);
$summary = [
'sub_total' => $sub_total,
'pajak' => $pajak,
'total' => $total
];
return view('livewire.cart-component', [
'products' => $products,
'carts' => $cartData,
'summary' => $summary
]);
}
public function addItem($id){
$rowId = "Cart".$id;
$cart = \Cart::session(Auth()->id())->getContent();
$cekItemId = $cart->whereIn('id', $rowId);
if($cekItemId->IsNotEmpty()){
\Cart::session(Auth()->id())->update($rowId, [
'quantity' =>[
'relative' => true,
'value' => 1
]
]);
}else{
$product = Product::findOrFail($id);
\Cart::session(Auth()->id())->add([
'id' => "Cart".$product->id,
'name' => $product->name,
'price' => $product->harga,
'quantity' => 1,
'attributes' =>[
'added_at' => Carbon::now()
],
]);
}
这里是产品刀片代码
<div class="container project-items mt-4 mb-5">
<div class="row justify-content-center">
@foreach ($products as $product)
<div class="col-lg-6 col-md-7 mb-4">
<div class="card">
<img class="card-img-top" src="{{ asset('produkListJasa') }}/{{$product->image}}" alt="{{$product->name}}">
<div class="card-body">
<p class="text-center font-weight-bold card-text">{{$product->name}}</p>
<div class="text-center font-weight-bold wrap-price"><span class="product-price">Rp. {{$product->harga}}</span></div>
<!-- Buttonnya percobaan doang -->
<button wire:click="addItem({{$product->id}})" class="btn btn-primary btn-sm btn-block">Add To Cart</button>
</div>
</div>
</div>
@endforeach
</div>
</div>
这是我的购物车刀片代码,这样当点击产品按钮时,产品和价格就会进入这个购物车刀片
<div class="container">
<div class="row">
<div class="card">
<div class="card-body">
<h2 class="font-weight-bold text-center">Cart</h2>
<table class="table table-sm table-bordered table-striped table-hovered">
<thread>
<tr>
<th>No</th>
<th>Name</th>
<th>Price</th>
</tr>
</thread>
</table>
<tbody>
@forelse($carts as $index=>$cart)
<tr>
<td>{{$index + 1}}</td>
<td>{{$cart['name']}}</td>
<td>{{$cart['harga']}}</td>
</tr>
@empty
<td colspan="3"><h6 class="text-center font-weight-bold">Empty Cart</h6></td>
<div class="image d-flex mt-5 justify-content-center">
<img src="foto/cart_empty.png" alt="cart visual" width="1200" height="800">
</div>
@endforelse
</tbody>
</div>
</div>
</div>
</div>
我不知道它是来自 livewire 还是 laravel 或其他任何东西,我还在学习这个,只是在学习一些教程视频 我希望任何人都可以帮助我 谢谢
答案 0 :(得分:0)
在 addItem 方法中检查或 dd 每条语句的结果。我在这里看到了一些您必须检查的内容:
$rowId = "Cart".$id;
$cart = \Cart::session(Auth()->id())->getContent();
$cekItemId = $cart->whereIn('id', $rowId);
据我所知, whereIn 接收一个数组作为第二个参数,而你没有。所以检查这一切