public function store(OrderCreateRequest $request)
{
$formField = $request->validated();
$totalAmount = 0;
// get the price of products of product_id using whereIn
$products = DB::table('products')
->select('price')
->whereIn('id', $formField['product_id'])
->get();
foreach ($products as $product) {
$totalAmount += $product->price;
}
$order = Order::create([
'product_id' => $formField['product_id'],
'user_id' => Auth::id(),
'total' => $totalAmount,
'status' => false,
]);
$response = [
'message' => 'order placed',
'created' => new OrderResource($order),
];
return response($response, 201);
}
what is wrong with this piece of code?
error:
Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, int given,
$formField['product_id']must be an array of ids, if you really want to check if this id exists on an array of ids. But if you just want to compare if where the is is exists in the database, just usewhereId($formField['product_id'])orwhere('id', $formField['product_id']).