Front end:
<div class="cart-table table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th class="pro-thumbnail"><input type="checkbox" id="select-all" style="margin-right: 20px;"></th>
<th class="pro-title"></th>
<th class="pro-price"></th>
<th class="pro-quantity"></th>
<th class="pro-subtotal"></th>
<th class="pro-remove"></th>
</tr>
</thead>
<tbody>
@foreach($cartItems as $item)
<tr>
<td class="pro-thumbnail">
<input type="checkbox" name="select" value="{{ $item->productSku->id }}" {{ $item->productSku->on_slae ? "checked" : "disabled" }}>
<a href="-sharp">
<img class="img-fluid" src="{{ $item->productSku->product->image_url }}" alt="Product">
</a>
</td>
<td class="pro-title">
<a href="-sharp">
{{ $item->productSku->product->title }}
</a>
<div @if(!$item->productSku->product->on_sale) class="not_on_sale" @endif>
<span class="product_title">
<a target="_blank" href="{{ route("products.show", [$item->productSku->product_id]) }}" style="text-align: center">
{{ $item->productSku->title }}
</a>
</span>
@if(!$item->productSku->product->on_sale)
<span class="warning"></span>
@endif
</div>
</td>
<td class="pro-price"><span>{{ $item->productSku->price }}</span></td>
<td class="pro-quantity">
<div class="pro-qty">
<input type="text" @if(!$item->productSku->product->on_sale) disabled @endif name="amount" value="{{ $item->amount }}">
</div>
</td>
<td class="pro-subtotal"><span>{{ $item->productSku->price }}</span></td>
<td class="pro-remove"><a href="-sharp"><i class="fa fa-trash-o"></i></a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
Controller:
class CartController extends Controller
{
//
public function index(Request $request)
{
$cartItems = $request->user()->cartItems()->with(["productSku.product"])->get();
return view("carts.index",compact("cartItems"));
}
//
public function add(AddCartRequest $request)
{
$user = $request->user();
$skuId = $request->input("sku_id");
$amount = $request->input("amount");
//
if ($cart = $user->cartItems()->where("product_sku_id", $skuId)->first()) {
//
$cart->update([
"amount" => $cart->amount + $amount,
]);
} else {
//
$cart = new CartItem(["amount" => $amount]);
$cart->user()->associate($user);
$cart->productSku()->associate($skuId);
$cart->save();
}
return [];
}
}
current database:
public function up ()
{
Schema::create("cart_items", function (Blueprint $table) {
$table->increments("id");
$table->unsignedInteger("user_id")->comment("ID");
// user_id users,, ,.
$table->foreign("user_id")->references("id")->on("users")->onDelete("cascade");
$table->unsignedInteger("product_sku_id")->comment(" SKU ID");
$table->foreign("product_sku_id")->references("id")->on("product_skus")->onDelete("cascade");
$table->unsignedInteger("amount")->comment("");
//$table->timestamps();
});
effect:
is it necessary to add fields to the database-- "then write the corresponding method to the controller; or if there is any better way, please post it for some, thank you
"