Why does Laravel’s Eloquent insist that the foreign key in a hasOne relationship must be in the referenced table and not in the parent table?
I'm asking for general understanding / enrichment.
eg, from the Laravel docs: If a User has one Phone, the Phone model is expected to have a user_id foreign key.
This seems like more work for the database to do: to go and find the user_id in the Phones table, versus a phone_id already being present on the User model.
I'm assuming these three reasons below all apply - are there any other reasons to structure it in this way?
- It can utilise logic already written for looking up has-many relationships (where it’s obvious why it needs to be this way).
- The relationship can easily be ‘migrated’ to a has-many relationship.
- It guarantees that wherever a relationship exists, the
Phonemodel also does in fact exist. (Otherwise, aphone_idin theUsermodel could potentially point to a non-existent entry in thePhonestable.)