topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.1k stars 130 forks source link

Relate two different foreign keys to single local key #174

Open KonradHambuch opened 5 months ago

KonradHambuch commented 5 months ago

I have a table exchanges that has standard_parcel_id and return_parcel_id. I want my Parcel model have an Exchange, so my relationship would be:

public function exchange(){
        return $this->hasOne(Exchange::class, ['standard_parcel_id', 'return_parcel_id'], ['id', 'id']);
    }

In this case no parcel finds its exchanges table record. If I use only one 'id', it will work for the first foreign key, but not for the second one. So in this case:

public function exchange(){
        return $this->hasOne(Exchange::class, ['standard_parcel_id', 'return_parcel_id'], ['id']);
    }

standard parcels find their Exchange, but return parcels do not.

Is there a workaround here?