1
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,
1
  • Hi, the second parameter of whereIn should be an array, then this $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 use whereId($formField['product_id']) or where('id', $formField['product_id']). Commented Dec 26, 2023 at 18:03

1 Answer 1

3

You are giving an int to whereIn(), whereIn is a function to check a column value against multiple values.

->whereIn('status', ['pending', 'completed'])

In your case either product_id should be an array, or you should just use a simple where().

->where('id', $formField['product_id'])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.