staudenmeir / belongs-to-through

Laravel Eloquent BelongsToThrough relationships
MIT License
1.15k stars 88 forks source link

Custom foreign Key #71

Closed jmicarus12 closed 2 years ago

jmicarus12 commented 2 years ago

`public function city() { return $this->belongsTo(City::class); }

public function province() { return $this->belongsToThrough(Province::class, City::class); }

public function posting_city() { return $this->belongsTo(City::class, 'posting_city_id'); }

public function posting_province() { return $this->belongsToThrough(Province::class, City::class, 'posting_city_id'); }`

Is it possible to have a custom foreign key on belongsToThorugh ? I tried the current code but its not working.

staudenmeir commented 2 years ago

Do you mean like this?

public function posting_province()
{
    return $this->belongsToThrough(
        Province::class,
        City::class,
        null,
        '',
        [City::class => 'posting_city_id']
    );
}
jmicarus12 commented 2 years ago

Thank you, i thought it was only for deeper relationships.