Я использую Crinsane Laravel в своем проекте электронной коммерции. Я хочу добавить товары из корзины как заказ в таблицу order
. Я использовал функцию foreach
в OrderController:
use App\Models\Order;
public function __construct(Order $order){
$this->order = $order;
}
public function store(Request $request)
{
foreach(Cart::content() as $row){
$data['product_id'] = $row->id;
$data['price'] = $row->price;
$data['quantity'] = $row->qty;
$data['color_id'] = $row->options->color_id;
$data['size_id'] = $row->options->size_id;
$data['user_id'] = auth()->user()->id;
$data['order_id'] = $request->order_id;
$data['status'] = 'Processing';
$this->order->fill($data);
$this->order->save();
}
$notification = array(
'alert-type' => 'success',
'message' => 'Order submitted successfully.'
);
Cart::destroy();
$order_id = $request->order_id;
$order_detail = $this->order->with('products')->where('order_id', $order_id)->get();
return view('frontend.cod.success',compact('order_detail','notification','order_id'));
}
Но приведенный выше код сохраняет в базе данных только данные одного продукта. Что здесь не так?
Edit Результат dd(Cart::Content());
Illuminate\Support\Collection {#463 ▼
#items: array:2 [▼
"adc28c54b34a48fc6a61151579c914ba" => Gloudemans\Shoppingcart\CartItem {#464 ▼
+rowId: "adc28c54b34a48fc6a61151579c914ba"
+id: 114
+qty: 1
+name: "Air Cushion Damping Boys Sneakers Soft Bottom Sports Shoes 485"
+price: 2000.0
+options: Gloudemans\Shoppingcart\CartItemOptions {#465 ▼
#items: array:4 [▼
"image" => "https://example.com/uploads/IMG_0939.png"
"slug" => "air-cushion-damping-boys-sneakers-soft-bottom--sports-shoes-485"
"color_id" => "5"
"size_id" => "195"
]
}
-associatedModel: "App\Models\Product"
-taxRate: 0
-isSaved: false
}
"ed928a560633f9bab8a0d1ed5bb0ca20" => Gloudemans\Shoppingcart\CartItem {#466 ▼
+rowId: "ed928a560633f9bab8a0d1ed5bb0ca20"
+id: 74
+qty: 1
+name: "ladies fashionable shoes"
+price: 2000.0
+options: Gloudemans\Shoppingcart\CartItemOptions {#467 ▼
#items: array:4 [▼
"image" => "https://example.com/uploads/-31854440.jpg"
"slug" => "ladies-fashionable-shoes"
"color_id" => "5"
"size_id" => "201"
]
}
-associatedModel: "App\Models\Product"
-taxRate: 0
-isSaved: false
}
]
}