Home > Software design >  SQL Statement: Integrity constraint violation:
SQL Statement: Integrity constraint violation:

Time:12-15

I am trying to check if a user has an order which links with order_items table and that order_items table has the product while ensuring same user ID and order ID. How would I go about doing this?

I have attempted to do this:

 $verify_order = Order::where('user_id', $user_id)
                ->join('order_items', 'orders.id', 'order_items.order_id')
                ->where('order.items.product_id', $product_id)->get(); 

but I get an error message of:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'user_id' in where clause is ambiguous

Orders Table

Order_items table

Products Table

CodePudding user response:

Orders and orders_item have both redundant user_id as column.

Best would be to remove the column from orderrs_item

or add the table name to the user_id

 $verify_order = Order::where('orders.user_id', $user_id)
                ->join('order_items', 'orders.id', 'order_items.order_id')
                ->where('order.items.product_id', $product_id)->get(); 
  • Related