staudenmeir / belongs-to-through

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

BelongsTo > HasOne Relationship #60

Closed jvbalcita closed 3 years ago

jvbalcita commented 3 years ago

Hi just wanted to ask if there's a way to connect belongsTo > HasOne relationship using the BelongsToThrought Trait.

I have a Admission Request > BelongsTo > Course > HasOne > PaymentTypes

Thank you for your answer! :)

staudenmeir commented 3 years ago

Please share the structure of all three tables.

jvbalcita commented 3 years ago

Hi @staudenmeir ,

Course id

Admission Request course_id

Payment Type course_id

Admission Request > BelongsTo > Course > HasOne > PaymentTypes

staudenmeir commented 3 years ago

You can actually do this with a native HasOneThroughrelationship:

public function paymentTypes()
{
    return $this->hasOneThrough(
        PaymentType::class,
        Course::class,
        'id',
        'course_id',
        'course_id',
        'id'
    );
}
jvbalcita commented 3 years ago

This worked thank you @staudenmeir