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

h

我正在尝试获取产品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'

任何帮助将不胜感激。

马格努斯·埃里克森

您实际上已经将产品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'],
    ]);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Firebase中的实时数据库检索数据?

如何从数据库中打印检索到的数据

如何在Firestore数据库中检索document.id?

我们如何使用vue js从数据库中检索产品计数?

如何在ASP.NET Core 3中从具有全文本索引的数据库中检索产品

如何从数据库中检索样本?

如何使用django从mongodb数据库中检索数据?

java如何从数据库中检索数据

如何通过当前字段从数据库检索对象

如何从数据库中检索数据?

我如何从mysql数据库中检索日期并减去当前日期?

如何使用yii从phpmyadmin数据库中检索数据

从MVC中的数据库中检索ID

如何根据当前用户ID查询Realm数据库?

如何从数据库中检索标签?

Laravel-PHP如何在数据库中插入值时获取当前ID?

如何在Wordpress数据库中检索保存的数据

使用JSON从数据库中检索不同的ID

从具有ID值的数据库中检索数据

如何从数据库中获取asp.net中当前用户的id

如何从数据库中检索资源?

使用数据库中的查询检索 ID

如何从 Adonis.js 中的数据库表中检索最新 id?

如何打乱从数据库中检索到的数据

如何在laravel的数据库中检索刚刚创建的id?

如何从 angular 9 的 firebase 实时数据库中检索单个产品详细信息并以 html 打印?

如何使用 AngularFire 从 Firebase 数据库中检索 ID

如何从 Firestore 数据库中检索数据

[SQL][数据库]SQL中如何统计每个产品star的review的id数?