Laravel hasManyThrough equivalent: belongsTo relationship through another model

BenjaminRH picture BenjaminRH · Apr 29, 2014 · Viewed 9.5k times · Source

I've got a model, it belongs to another model, that model belongs to a third model, and I want an eloquent method to relate the first model to the third one.

There doesn't appear to be a belongsToThrough (or hasOneThrough) method, though. I've already tried chaining multiple belongsTo methods, but that hasn't worked (Call to undefined method Illuminate\Database\Query\Builder::belongsTo()). Any ideas?

Here is an example of the models:

// The first model
// Schema: this model has a middle_id column in the database
class Origin extends Eloquent {
    public function middle()
    {
        return $this->belongsTo('Middle');
    }
}

// The second model
// Schema: this model has a target_id column in the database, but NOT an origin_id column
class Middle extends Eloquent {
    public function target()
    {
        return $this->belongsTo('Target');
    }
}

// The third model
class Target extends Eloquent {
}

What I'd like to do is add something like the following method to the Origin model:

// A relationship method on the first "origin" model
public function target()
{
    // First argument is the target model, second argument is the middle "through" model, third argument is the database column in middle model that it uses to find the target model, or soemthing
    return $this->hasOneThrough('Target', 'Middle', 'target_id');
}

So that I can use $originInstance->target->title, etc.

Answer

Razor picture Razor · May 1, 2014
public function target() { 
    $middle = $this->belongsTo('Middle', 'middle_id'); 
    return $middle->getResults()->belongsTo('Target'); 
}

Update:

Starting from laravel 5.8 you can use the hasOneThrough relationship:

public function target() { 
    return $this->hasOneThrough('Target', 'Middle');
}