joh
joh

Reputation: 238

How can I retrieve the current product id from database?

I'm trying to get product id, so I can save to order_product table. But I'm stuck.

Here is my store function in checkout Controller

//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');
    }
}

}

I need to be able to save current product_id associated with the order in the database. Because right now it shows

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

Any help will be appreciated.

Upvotes: 1

Views: 842

Answers (2)

Etin
Etin

Reputation: 365

Since each item in your cart array is indexed by a product ID, you can get the index within the foreach loop.

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

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

You also likely don't need the null coalescing operator since you're already checking if $order is a truthy value at the beginning.

Upvotes: 0

M. Eriksson
M. Eriksson

Reputation: 13635

You're actually already saving the product id as the array index:

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

So if you want to get that id in your foreach loop, use the format:

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

Your code would then be:

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

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

Upvotes: 1

Related Questions