Should i use belongsTo or hasOne in Laravel?

Laravel

Laravel 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

  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.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwolfgangView Question on Stackoverflow
Solution 1 - Laraveluser1669496View Answer on Stackoverflow
Solution 2 - LaravelAbid AliView Answer on Stackoverflow
Solution 3 - LaravelbilogicView Answer on Stackoverflow
Solution 4 - LaravelEduardo RomeroView Answer on Stackoverflow
Solution 5 - LaravelMuhammad KashifView Answer on Stackoverflow