115
votes

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?

5

5 Answers

262
votes

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.

98
votes

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.

3
votes

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

  1. A belongsTo B, B hasMany A
  2. B belongsTo A, A hasMany B
  3. A hasOne B, B hasMany A <- this will cause problems with both sides using hasXXX, see link.
0
votes

"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).

-1
votes

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.