Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kevin73's avatar

relationship between 3 tables with Eloquent

Hi , First, let's examine the table structure:

 estimate
    id - integer
    order_id - integer
    description - string

orders
    id - integer
    estimate_id - integer
    amount - int

customers
    id - integer
    firstname - string
    lastname - string

Models :

  • Estimate
class Estimate extends Model
{

    protected $table = 'estimate';

    public function order()
    {
        return $this->belongsTo('App\Models\Order','order_id');
    }
 
}

Order

class Order extends Model
{

    protected $table = 'order' ;

    public function customer()
    {
        return $this->belongsTo('App\Models\Customer', 'customer_id');
    }
 
}

Client

class Customer extends Model
{

    protected $table = 'customer' ;
 
}

when i tried :

public function index()
   {
       return response()
           ->json([
               'model' => Estimate::with('order','customer')->paginate(15)
           ]);
   }

I get a error :

Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer.estimate_id' in 'field list 

how to fix with it with Eloquent in the cleanest way ? Thank you in advance

0 likes
12 replies
tykus's avatar

Estimate hasOne or hasMany Order, not Estimate belongsTo Order. Also, there is nothing relating the Customer to either Order or Estimate:

// Estimate.php
public function order()
{
    return $this->hasOne(Order::class);
}


public function order()
{
    return $this->belongsTo(Customer::class);
}

For the Customer relation, you will need a customer_id FK on the estimates table.

1 like
kevin73's avatar

I just update my table structure , you can check , I had forgot to add order_id

tykus's avatar

Ok, good. Still the issue remains that there is no relation between Estimate and Customer, (or indeed Customer and Order!). The lack of a customer_id FK on those tables means you cannot establish a relationship between those Models

kevin73's avatar

so if i add the relation between Estimate and Order and between Order and Customer i get it:

 estimate
    id - integer
    order_id - integer
    description - string

orders
    id - integer
    customer_id - integer
    amount - int

customers
    id - integer
    firstname - string
    lastname - string

estimate > orders > customers

but i have the same error with eloquent

tykus's avatar

You are using the Estimate model to eager load a Customers relationship but there is still nothing on the `estimat table to associate an Estimate directly with a Customer.

You can get Customer as a nested relation on the Order model using the . syntax:

Estimate::with('order.customer')->findOrFail($id)
kevin73's avatar
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order.estimate_id' in 'where clause' (SQL: select * from `order` where `order`.`estimate_id` in (1))
class estimateController extends Controller
{
   public function index()
   {
       return response()
           ->json([
               'model' => Estimate::with('order.customer')->paginate(15)
           ]);
   }
}
tykus's avatar

You have changed the original schema; the estimates table did not have a order_id column whenever you first posted this? You do not need a FK on both orders and estimates tables!!!

Can you post the exact schema you are working with now, and the relationship you have defined?

kevin73's avatar

yes i edited , please checkout : correct structure table

 estimate
    id - integer
    order_id - integer
    description - string

orders
    id - integer
    customer_id - integer
    amount - int

customers
    id - integer
    firstname - string
    lastname - string
class Estimate extends Model
{

    protected $table = 'estimate';

    public function order()
    {
        return $this->belongsTo('App\Models\Order','order_id');
    }
}
class Order extends Model
{

    protected $table = 'order' ;

    public function customer()
    {
        return $this->belongsTo('App\Models\Customer', 'customer_id');
    }
 
}
class Customer extends Model
{

    protected $table = 'customer' ;
 
}
tykus's avatar
tykus
Best Answer
Level 104

That should be working; an Estimate belongs to an Order, and an Order belongs to a Customer whenever traversing the relationship from Estimate, you should get the related Order and Customer.

1 like

Please or to participate in this conversation.