The table structure given in the docs for a HasManyThrough relationship between a Source model, an Intermediate model and a Final model have the specific structure that Intermediate->belongsTo(Source) and Final->belongsTo(Intermediate).
I have an existing database where there’s a similar setup, except that in my case, Intermediate->belongsTo(Final), rather than the other way around. In other words, my intermediate table looks like this:
intermediates
id - integer
source_id - integer
final_id - integer
I have all the relationships between the directly related models set up, so this works as expected:
$sources = Source::with('intermediates.finals')->get();
This allows me to access $source->intermediates[$i]->finals, for example, but I would like to be able to access the finals directly through the source model. Since the individual relationships work, I naïvely thought that this would too:
// Source.php
public function finals() {
return $this->through('intermediates')->has('finals');
}
// Controller
$models = Source::with('finals')->get();
– but it doesn’t. It throws an error: Call to undefined relationship [finals] on model [Source].
Is there a way to make such a HasManyThrough relationship work?