I have a parent model called Journey that should relate to two instances of model-type Address - once for 'origin' and once for 'destination'. I can't work out what's the most efficient way of doing this. I would like to use Laravel's Eloquent ORM with eager-loading to achieve something elegant like the following when I return a journey:
return Journey::with('origin','destination');
I've tried the following:
1) Using Eloquent ORM's one to one relationships
Journey
-------
id
user_id
date
Address
-------
id
journey_id
street
city
zipcode
Observations:
- Requires 'journey_id' in the Address model, meaning I can't really re-use this model for anything other than Journeys
- Address is linked to Journey as a whole, so I can't distinguish between origin and destination instances
2) Using Eloquent ORM's polymorphic relationships
Journey
-------
id
user_id
date
Address
-------
id
street
city
zipcode
addressable_id
addressable_type
Observations:
- I can now re-use the Address model with other models, but...
- Address is still linked to Journey as a whole, so I can't distinguish between origin and destination instances
3) Relating 'journey.origin' or 'journey.destination' by joining it to 'address.id'
Journey
-------
id
user_id
origin (address.id)
destination (address.id)
date
Address
-------
id
street
city
zipcode
Observations:
- I can now distinguish between the origin & destination instances, but...
- I think I have to use fluent queries for the joins, so I don't get origin & destination back as Address models
- There is no link back from the address to the journey
Does anyone have any suggestions for table / relationship structure that makes journey (with origin & destination) insertion and selection as elegant as possible, preferably using Eloquent so that I can utilise my model attributes and functions?
Also, although accessing the addresses through the Journey model will be the primary use case, I'd also like to be able to access the parent Journey through the Address model.