I need an help understanding Eloquent has many relation in this particular scenario.
This is the scenario:
Airport Table
| id | code | name |
---------------------
| 1 | XXX | XXX |
---------------------
Route Table
| id | from | to |
-------------------
| 1 | 1 | 2 |
--------------------
As above, ONE airport table 'id' can be a foreign key in 'from' OR 'to' Route Table column.
What I'd like to do is to retrieve all records from Airports table with its routes (Where the airport can appear as a departure or as a destination).
For example:
[
{
'id': 1,
'code': 'JFK',
'name': 'John F. Kennedy International Airport'
'routes' : [
{
'id': 1,
'from': 1 //JFK airports table id
'to': 22 //somewhere
},
{
'id': 1,
'from': 334 //somewhere
'to': 1 //JFK airports table id
}
]
}
]
Obviously I know how to do it with a SQL query or using Eloquen but what I want to understand is if it can be done natively in the Model by expressing a relationship.