Should i use belongsTo or hasOne in Laravel?
LaravelLaravel Problem Overview
Consider two models A
and B
A
-> relatedTo B
is a one to one
relationship
What is the difference in using (A ->hasOne
-- B) and (A ->belongsTo
-- B)?
Can I use them interchangeably?
Laravel Solutions
Solution 1 - Laravel
No, the difference depends on where your foreign key is.
In your example, if A
has a b_id
column, then A
belongsTo
B
.
If B
has an a_id
column, then A
hasOne
or hasMany
B
depending on how many B
should have.
Solution 2 - Laravel
Main difference is as below:
belongsTo
and belongsToMany
- you're telling Laravel that this table holds the foreign key that connects it to the other table.
hasOne
and hasMany
- you're telling Laravel that this table does not have the foreign key.
Solution 3 - Laravel
This is not about where the foreign key is.
The OP brought up 2 scenarios: A hasOne
B, A belongsTo
B
For a contextual example, let A = user and B = company.
> You can say "user owns(hasOne
) a company" and conversely you can say "user worksAt(belongsTo
) a company".
> In both scenarios, user
has a company_id
field
Please let that sink in for a moment. Thus, discussing the location of foreign key is a non starter. The most apt answer can be found here: https://laravel.io/forum/04-20-2015-belongsto-vs-hasone
> although it works, it is an incorrect description of the relationship.
To answer the OP's question: No, you should not use them interchangeably.
They are usually used in pairs
- A
belongsTo
B, BhasMany
A - B
belongsTo
A, AhasMany
B - A
hasOne
B, BhasMany
A <- this will cause problems with both sides usinghasXXX
, see link.
Solution 4 - Laravel
"BelongsTo" goes in the table with the "xxx_id" column. Example: A Country has Cities. A City belongsTo a Country (country_id in cities table). Another Example: A Face hasOne Nose. A Nose belongsTo a face (face_id in noses table).
Solution 5 - Laravel
The model who's table contains the foreign key will have belongsTo() in it while the model who's table contains that primary key to which that foreign key is referencing to will have hasOne()... its easy the model that has the foreign key will have belongsTo() and the one that doesn't contain the foreign Key in that relationship will have hasOne(). and no they are not interchangeable using the wrong method will always return null as a result.