How to set Eloquent relationship belongsTo THROUGH another model in Laravel?

How to set Eloquent relationship belongsTo THROUGH another model in Laravel?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Eloquent Relationships Through Intermediate Models

When designing database schemas where a record belongs to another entity which, in turn, belongs to a third party, standard belongsTo relationships often fall short. Consider a scenario involving three tables: listings, models, and manufacturers. A listing is directly associated with a specific model via a model_id foreign key. That model, however, holds the manufacturer_id. You naturally want your Listing model to expose a direct manufacturer() accessor, avoiding the need to manually chain $listing->model->manufacturer.

The initial attempt usually looks like this in the Listing model:

public function manufacturer()
{
    return $this->belongsTo('Manufacturer', 'models.manufacturer_id');
}

This syntax is invalid. The second argument of belongsTo expects a foreign key column name on the current table, not a dotted path to a relationship. Eloquent cannot infer that you are looking for the manufacturer through the intermediate Model relation without explicit instruction. Trying to access $listing->manufacturer->name with this setup throws an error because Eloquent doesn't know how to construct the query.

A common workaround found in some forums suggests manually resolving the relationship before returning it:

public function model()
{
    return $this->belongsTo('Model', 'model_id');
}

public function manufacturer()
{
    $manufacturer_id = $this->model->manufacturer_id;
    // This is not a relationship definition, but a direct query.
    // It breaks the lazy-loading benefits of Eloquent and couples your logic tightly to specific query results.
    return Manufacturer::find($manufacturer_id)->name; 
}

While this fixes the immediate access issue, it violates the principle of defining relationships declaratively. It also fails if $this->model is null (e.g., during eager loading or if a listing exists without a model), causing fatal errors. You need to tell Eloquent exactly how to traverse the join path.

Using hasManyThrough for Inverse Navigation

The most robust solution when navigating from the child up to the grandparent using an intermediate table is often to reverse the perspective in your Manufacturer model. If you have a Manufacturer model, define the relationship there:

public function listings()
{
    return $this->hasManyThrough(
        'Listing', 
        'Model', 
        'manufacturer_id', // Foreign key on Model table that belongs to Manufacturer
        'model_id'         // Foreign key on Listing table that belongs to Model
    );
}

This configuration tells Eloquent: "To get a Listing for me, go through the Model table." This allows you to access $manufacturer->listings directly. However, if your specific requirement is strictly accessing the manufacturer from within the Listing model without reversing the direction, you must use the through property on the belongsTo method itself.

Configuring belongsTo with an Intermediate Model

To keep the relationship definition on the Listing model while traversing the intermediate table, update your accessor to explicitly define the pivot:

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class, 'model_id', 'manufacturer_id')
        ->through(Model::class);
}

This syntax is cleaner and more declarative than manual joins. Here is how it works under the hood:

  1. First Argument: The related model (Manufacturer).
  2. Second Argument: The foreign key on the listings table pointing to the intermediate model (model_id). Note that in this specific schema, the primary key of Model might be an ID, but the relationship is defined by the foreign key model_id. If your primary keys differ from foreign keys, ensure the order matches: (Related, ForeignKeyOnCurrent, LocalKeyOnIntermediate).
  3. Third Argument: The intermediate model class (Model).

Wait, looking at the source material provided in the prompt, there was an attempt to use join(). While join() works for raw queries or specific scopes, it is generally discouraged for defining relationships because it mixes query logic with relationship definition. The standard Laravel way to handle this "through" relationship is the ->through() method introduced in recent versions (Laravel 8+), or constructing the full dotted path if using older syntax variations, though hasManyThrough remains the most explicit solution for multi-level traversals.

If you are constrained to an older version of Laravel that lacks native ->through() support on belongsTo, or if you prefer a more explicit SQL-like definition within Eloquent, you can define the relationship as follows:

public function manufacturer()
{
    return $this->belongsToMany(Manufacturer::class, 'models', 'model_id', 'manufacturer_id')
        ->through(Model::class); 
}

Correction: The belongsToMany approach assumes a many-to-many relationship which might not fit your schema. For a strict hierarchy (One Listing -> One Model -> One Manufacturer), the hasManyThrough approach on the parent (Manufacturer) is the canonical pattern. If you must define it on Listing, ensure your foreign keys align perfectly with the intermediate table's primary key.

Trade-offs and Best Practices

The primary trade-off here is flexibility versus explicitness. Defining the relationship on the Manufacturer model using hasManyThrough is often preferred because it encapsulates the join logic in one place. You avoid cluttering the Listing model with complex query definitions that rely on foreign keys of other tables.

However, if your application architecture demands that every entity knows its ultimate owner (perhaps for security scopes or audit logging), defining it on the Listing model is acceptable. Just be careful with eager loading:

$listing = Listing::with(['model', 'manufacturer'])->find($id);

Eloquent will automatically handle the joins required to populate $listing->manufacturer, provided the relationship is defined correctly using the intermediate model. Always verify your foreign key column names match your database schema exactly; a mismatch here is the most common cause of silent failures or "relation does not exist" errors.

Our network