简体   繁体   English

电子商务购物车在laravel中一次又一次地添加相同的产品ID

[英]Ecommerce cart adding same product id again and again in laravel

I have three products in my products table.我的产品表中有三个产品。

ID- 11, 12 and 13 ID- 11、12 和 13

But when I am trying to add all the three products into cart, only id-13 product is being added without quantity increment.但是,当我尝试将所有三种产品添加到购物车时,仅添加了 id-13 产品而没有增加数量。 Cant add product 11 and 12. Please help me with the code.无法添加产品 11 和 12。请帮我提供代码。

Here is my CartController-这是我的 CartController-

public function store(Request $request)
    {

        if (Auth::check()) {

            $cart = new Cart;

            if($cart->product_id == $request->product_id){
                $cart->increment('product_id');
            }
            else{
                $cart->user_id = Auth::id();
                $cart->product_id = $request->product_id;
                $cart->save();
            

            session()->flash('success', 'Product has added to cart !!');
            return back();
            }
        }
    }

Here is addtocart.blade.php -这是 addtocart.blade.php -

<form action="{{route('cart.store')}}" method="post">
@csrf
@foreach($products as $product)
    <input type="hidden" name="product_id" value="{{$product->id}}">
    @endforeach
  <button type="submit" class="btn btn-light">Add to cart</button>
</form>

Here is my cart table image - Cart table这是我的购物车表图片 -购物车表

Please help.请帮忙。

First you need to check the rows of the cart table try this首先你需要检查购物车表的行试试这个

$cartRow = Cart::where('user_id',Auth::id())->where('product_id',$request->product_id)->first();
if($cartRow){
    $cartRow->increment('product_quantity');
}else{
    Cart::create(['user_id'=>Auth::id(),'product_id'=>$request->product_id]);
}

The first problem is that you're creating 3 different input elements with the same name, so only one of the values is being taken.第一个问题是您正在创建 3 个具有相同名称的不同输入元素,因此只有一个值被采用。 An alternative could be create radio buttons:另一种方法是创建单选按钮:

<form action="{{route('cart.store')}}" method="post">
    @csrf
    @foreach($products as $product)
        <input type="radio" name="product_id" value="{{$product->id}}">
        <label>Product {{$product->id}}</label>
    @endforeach
    <button type="submit" class="btn btn-light">Add to cart</button>
</form>

This way only one id will be sent to your controller (the one selected).这样,只有一个 id 将发送到您的控制器(选定的那个)。

The other problem is in your controller, you're creating a new instance of cart with every call, so there won't be any increment,另一个问题是在您的控制器中,您在每次调用时都会创建一个新的 cart 实例,因此不会有任何增量,

$cart = Cart::where('user_id',Auth::id())
    ->where('product_id',$request->product_id)
    ->first();

Then you can increment if $cart exists or create a new record然后你可以增加如果 $cart 存在或创建一个新记录

if($cart){
    $cart->increment('product_quantity');
    $cart->save();
}else{
    Cart::create([
        'user_id' => Auth::id(),
        'product_id' => $request->product_id
    ]);
}

So it worked.所以它奏效了。 Removed foreach loop from addtocart.blade.php as it is partially included to products.blade.php.从 addtocart.blade.php 中删除了 foreach 循环,因为它部分包含在 products.blade.php 中。

Before -前 -

<form action="{{route('cart.store',$product->id)}}" method="post">
@csrf
  @foreach($products as $product)
    <input type="id" name="product_id" value="{{$product->id}}">
   @endforeach 
  <button type="submit" class="btn btn-light">Add to cart</button>
  
</form>

Before image前图

After -后 -

<form action="{{route('cart.store',$product->id)}}" method="post">
@csrf
    <input type="id" name="product_id" value="{{$product->id}}">
  <button type="submit" class="btn btn-light">Add to cart</button>
  
</form>

After image后图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM