如何从数据库中检索当前的产品ID?

时间:2019-06-13 11:39:38

标签: php laravel laravel-5 eloquent

我正在尝试获取产品ID,因此可以保存到order_product表中。但是我被卡住了。

这是我在结帐控制器中的存储功能

//Insert into order product table
if ($order) {
    foreach(session('cart')  as $item) {
        if (empty($item)) {
            continue;
        }

        OrderProduct::create([
            'order_id' => $order->id ?? null,
            'product_id' =>DB::table('products')->get('id'),
           // $products=DB::table('products')->where('id',$id)->get();
            'quantity' => $item['quantity'],
        ]);
   }
}




CartController

   public function cart()
{
    return view('cart');
}

public function addToCart($id)
{
    $userId = Auth::id();
    $products = Products_model::find($id);
    if(!$products) {

        abort(404);

    }

    $cart = session()->get('cart');

    // if cart is empty then this the first product
    if(!$cart) {

        $cart = [
                $id => [
                    "pro_name" => $products->pro_name,
                    "quantity" => 1,
                    "pro_price" => $products->pro_price,
                    "image" => $products->image
                ]
        ];

        session()->put('cart', $cart);

        return redirect()->back()->with('success', 'Product added to cart successfully!');
    }

    // if cart not empty then check if this product exist then increment quantity
    if(isset($cart[$id])) {

        $cart[$id]['quantity']++;

        session()->put('cart', $cart);

        return redirect()->back()->with('success', 'Product added to cart successfully!');

    }

    // if item not exist in cart then add to cart with quantity = 1
    $cart[$id] = [
        "pro_name" => $products->pro_name,
        "quantity" => 1,
        "pro_price" => $products->pro_price,
        "image" => $products->image
    ];

    session()->put('cart', $cart);

    return redirect()->back()->with('success', 'Product added to cart successfully!');
}

public function update(Request $request)
{
    if($request->id and $request->quantity)
    {
        $cart = session()->get('cart');

        $cart[$request->id]["quantity"] = $request->quantity;

        session()->put('cart', $cart);

        session()->flash('success', 'Cart updated successfully');
    }
  }

public function remove(Request $request)
{
    if($request->id) {

        $cart = session()->get('cart');

        if(isset($cart[$request->id])) {

            unset($cart[$request->id]);

            session()->put('cart', $cart);
        }

        session()->flash('success', 'Product removed successfully');
    }
}

}

我需要能够将与订单相关联的当前product_id保存在数据库中。因为现在它显示

Incorrect integer value: '[{"id":1},{"id":2}]' for column 'product_id'

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您实际上已经将产品ID保存为数组索引:

$cart = [
    $id => [ // $id is the product id
        // ...
    ]
];

因此,如果您想在foreach循环中获取该ID,请使用以下格式:

foreach ($array as $index => $value)

您的代码应为:

foreach (session('cart') as $productId => $item) {
    if (empty($item)) {
        continue;
    }

    OrderProduct::create([
        'order_id'   => $order->id ?? null,
        'product_id' => $productId,
        'quantity'   => $item['quantity'],
    ]);
}

答案 1 :(得分:0)

由于购物车数组中的每个商品都由产品ID进行索引,因此您可以在foreach循环中获取索引。

 if ($order) {
    foreach(session('cart')  as $productId => $item) {
        if (empty($item)) {
            continue;
        }

        OrderProduct::create([
            'order_id' => $order->id,
            'product_id' =>$productId,
            'quantity' => $item['quantity'],
        ]);
   }
}

您还可能不需要null合并运算符,因为您已经在开始检查$order是否为真值。