Skip to content
Advertisement

Column not found 1054 error when defining laravel relationship

I have two tables: Order and Order_details. Between these two tables, there is a one-to-many relationship.

Order fields are:

  • id_order

  • invoice

  • customer_id

  • user_id

  • total

Order_details fields:

  • order_detail_id
  • order_id
  • product_id
  • qty
  • price

order_id is a foreign key which references to id_order on orders table

Order Model

JavaScript

Order_detail model

JavaScript

When i tried to insert data to these table, i got an error

JavaScript

Why laravel assume i have order_id_order field instead of order_id? And how to fix it?

Thank you

Advertisement

Answer

Here, in your question, orders table is the parent table and order_details is the child table. So one order can have many order details. So there is one-to-many relationship.

In Order.php i.e order model, you need to have forward relation, with proper mapping with the primary key and foreign key.

JavaScript

Now coming to OrderDetail.php i.e. order_detail model, you need to have inverse relation as given below.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement