首页 > 解决方案 > 产品正在更新而不是新产品插入 laravel?

问题描述

麻烦你看看我的问题...

我设计了三类产品

  1. Sp 2. Hp 3. Gp

三个类似图像的菜单(包括在内)。

在此处输入图像描述

我已经完成了我的代码......

<table class="table table-bordered">
<!-- Table Header -->
<thead>
<tr>
<th>#</th>
<th>Image</th>
<th>Product Name</th>
<th>Net Weight</th>
<th>Category</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>

@php 
$all_product = DB::table('products')
            ->join('categories', 'products.cat_id', '=', 'categories.id')
           ->where('cat_name', 'SP Products')
            ->get();
@endphp

@foreach($all_product as $row)      
                                        

<tr>
    <form action="{{ url('/add-to-cart') }}" method="POST">
                                        @csrf
    
<input type="hidden" name="product_id" value="{{ $row->id }}">

<input type="hidden" name="product_name" value="{{ $row->product_name }}">

<input type="hidden" name="product_qty" value="1">

<input type="hidden" name="product_price" value="{{ $row->selling_price }}">

<input type="hidden" name="product_weight" value="{{ $row->net_weight }}">

<input type="hidden" name="product_image" value="{{ $row->product_image }}">

  <td>{{ $row->product_serial }}</td>
<!-- Product image -->
<td>
<a href="#">
<img src="{{ $row->product_image }}" alt="" class="img-responsive" style="width: 30px; height: 30px;" />
                                                </a>
</td>
<td><a href="#"> {{ $row->product_name }} </a></td>
<td>{{ $row->net_weight }}</td>
    <td>{{ $row->cat_name }}</td>
<td>{{ $row->selling_price }}</td>



    <td><button type="submit" class="btn btn-warning btn-sm"> Add to Cart</button></td>
</form>
                                            </tr>
    
@endforeach
                                        
                                        </tbody>
                                    </table>

在 web.php 中

Route::post('/add-to-cart', 'front\AddtoCartController@add_to_cart')->name('add_to_cart');

AddtoCartController.php

public function add_to_cart(Request $request)
    {
        $qty = $request->product_qty;
        $product_id = $request->product_id;
        $product_info=DB::table('products')->where('id', $product_id)->first();

        $data['qty']=$qty;
        $data['id']=$product_info->id;
        $data['name']=$product_info->product_name;
        $data['price']=$product_info->selling_price;
        $data['weight']=0;
        $data['options']['image']=$product_info->product_image;

        Cart::add($data);
        return Redirect::to('show-cart');

    }

当我将产品添加到购物车时,正确的产品没有添加到购物车,添加另一个产品并增加另一个产品数量。

请帮忙。谢谢

标签: phplaravelcart

解决方案


推荐阅读