xamshah's avatar

I want to add product ID in OrderProduct Table but I'm not be able to do that.

When I'm trying to put product_id it shows Undefined index: id.

  $cart = Session::get('cart');
        $quantity=0;
        $price=0;

        foreach($cart as $item){
            $product_id = $item['id'];
            // dd($product_id);
            $quantity=$item['quantity'];
            $price=$item['price'];
            $total_price = $item['quantity'] * $item ['price'];

       OrderProduct::insert([

            'order_id'  => $order_id,
            // 'product_id'=> $item['id'],
            'product_id'=> $product_id,
            'quantity' => $quantity,
            'price' =>  $price,
            'total_price' => $total_price,
            'created_at' => Carbon::now(),
        ]);
     }
0 likes
8 replies
Sinnbeck's avatar

We need to know what you are working with. Show the output of dd($cart);

xamshah's avatar

@Sinnbeck here you go

array:2 [▼
  5 => array:4 [▼
    "name" => "Black crop top shirt"
    "quantity" => "1"
    "price" => "15"
    "image" => "/storage/images/1661249861product-10.jpg"
  ]
  7 => array:4 [▼
    "name" => "Spider Man grey shirt"
    "quantity" => "1"
    "price" => "4.15"
    "image" => "/storage/images/1661316825spiderkid.jpg"
  ]
]
tykus's avatar

@xamshah there is nothing to identify the Product in the Cart unless the keys 5 an 7 are the product IDs? You need to store the product_id in the Cart as well as the name (if that is needed at all).

Aside, the order_product table doesn't really need its own Model class? If you have an Order instance:

$order = Order::findOrFail($order_id);

Then you can add products to the Order from the Cart using a products BelongsToMany relationship:

// Order
public function products()
{
    return $this->belongsToMany(Product::class)->withTimestamps();
}

Assuming the key on the Cart is the product ID, then the products can be attached to the Order like this:

$order = Order::findOrFail($order_id);
$cart = Session::get('cart');

$products = collect($cart)
    ->mapWithKeys(function ($item, $product_id) {
        return [$product_id => [
            'quantity' => $item['quantity'], 
            'price' => $item['price'],  
            'total_price' => $item['quantity'] * $item ['price']
        ];
    })->all();
$order->products()->attach($products);
xamshah's avatar

@tykus You are making this way to complex brother like in "Order Table" I get Customer_ID from last inserted id of customer. and now here I just want to put "id" (product) in my column "product_id". In my dd($cart) you can see there are id's are given for product 5, 7. We need to only insert it in my column that's the work.

tykus's avatar

@xamshah you think this is complex??? I'm not sure software development is for you. Also, the last inserted customer might not be the right customer for the Order if two orders are being placed at the same time.

1 like
xamshah's avatar

@tykus Brother, I'm beginner, and If my words were wrong then I'm sorry. Every master was once a beginner.

tykus's avatar

@xamshah if you want to learn, then try to learn the conventional Laravel approach.

Please or to participate in this conversation.