mercredi 10 juillet 2019

Laravel 5.8 relation with 2 different foreign keys refer to the id in relationship table

I have a TransportWorkflowStep model and a Client model. I have a source_id and destination_id field in this TransportWorkflowStep model. These 2 fields should be a forgeign key to the id from the Client model.

So for example source_id refers to client_id 1 and destination_id refers to client_id 2

Here's an image of my database schema

Now when I am trying to set up this relation in Laravel I can only fill up 1 foreign key, either source_id or destination_id

TransportWorkflowStep model:

public function client(): BelongsTo
{
    return $this->belongsTo(Client::class, 'source_id', 'id');
}

Client model:

public function transportWorkflowSteps(): HasMany
{
    return $this->hasMany(TransportWorkflowStep::class, 'source_id', 'id');
}

Should I make a "BelongsTo" methods for every foreign key? I am not sure how I can make this relationship.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire