Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从不同的表将多个项目添加到一个购物车中_Php_Mysql_Sql - Fatal编程技术网

Php 如何从不同的表将多个项目添加到一个购物车中

Php 如何从不同的表将多个项目添加到一个购物车中,php,mysql,sql,Php,Mysql,Sql,我想添加多个项目到一个购物车,从两个不同的表,如自定义产品和产品。我可以将产品添加到购物车,但在将自定义产品添加到购物车时遇到问题。我希望你们理解我的问题,所以请指导我如何从两个不同的表将多个项目添加到一个购物车中 产品 身份证 名字 描述 供应商id 定制产品 身份证 风味 形状 描述 供应商id 推车 身份证 客户识别码 产品标识 客户产品标识 函数getCart Item $allCartItems = Cart::where([

我想添加多个项目到一个购物车,从两个不同的表,如自定义产品和产品。我可以将产品添加到购物车,但在将自定义产品添加到购物车时遇到问题。我希望你们理解我的问题,所以请指导我如何从两个不同的表将多个项目添加到一个购物车中

产品

  • 身份证
  • 名字
  • 描述
  • 供应商id
定制产品

  • 身份证
  • 风味
  • 形状
  • 描述
  • 供应商id
推车

  • 身份证
  • 客户识别码
  • 产品标识
  • 客户产品标识
函数getCart Item

          $allCartItems = Cart::where([
                    'customer_id' => $customerId
                ])->get()->map(function ($instance) {
                    $product = Product::find($instance->product_id);
                    $instance->product = $product;
                    $instance->quantity = 1;
                    return $instance;
                })->toArray();
    
                $subTotal = array_reduce($allCartItems, function ($acc, $instance) {
                    return $acc + $instance['product']['product_price'];
                }, 0);
    
                $mergedArr = [];
    
                foreach ($allCartItems as $key => $instance) {
                    $filter = function ($item) use ($instance) {
                        return $item['product_id'] == $instance['product_id'];
                    };
    
                    $existingItem = array_filter($mergedArr, $filter);
    
                    if ($existingItem) {
                        $mergedArr[sizeof($mergedArr) - 1]['quantity'] = $mergedArr[sizeof($mergedArr) - 1]['quantity'] + 1;
                    } else {
                        array_push($mergedArr, $instance);
                    }
                }
    
                $payload = (object)[];
                $payload->products = $mergedArr;
                $payload->subTotal = $subTotal;
    
                return  $payload;

        
函数AddtoCart

       function($Request request){
                   $customerId = $request->customer_id;
            $productId = $request->product_id;
    
            $product = Product::find($productId);
            $existingCart = Cart::where(['customer_id' => $customerId])->first();
    
            if ($existingCart) {
                $vendorId = $existingCart->product->vendor->id;
    
                if ($product->vendor->id !== $vendorId) {
                    throw new Exception('You cannot add product from different vendor.');
                }
            }
    
            Cart::create([
                'product_id' => $productId,
                'customer_id' => $customerId
            ]);
    
            $payload = getCartItem($customerId);
    
               $customerId = $request->customer_id;
            $productId = $request->product_id;
    
            $product = Product::find($productId);
            $existingCart = Cart::where(['customer_id' => $customerId])->first();
    
            if ($existingCart) {
                $vendorId = $existingCart->product->vendor->id;
    
                if ($product->vendor->id !== $vendorId) {
                    throw new Exception('You cannot add product from different vendor.');
                }
            }
    
            Cart::create([
                'product_id' => $productId,
                'customer_id' => $customerId
            ]);
    
            $payload = getCartItem($customerId);
    
            return response()->json([
                'success' => true,
                'message' => 'success!',
                'payload' => $payload
            ]);
        }

你的错误是什么?