65

Can any body tell me what is the main difference between
the BelongsTo and HasOne relationship in eloquent.

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Pardeep Pathania
  • 1,378
  • 2
  • 15
  • 23
  • [This explanation helped|http://inlehmansterms.net/2014/07/28/has_one-vs-belongs_to/] me understand the relationship better – Wesley Smith Oct 07 '19 at 16:40
  • Yes [that post from In Lehman's Terms _Database Associations- "Has One" vs "Belongs To"_](http://inlehmansterms.net/2014/07/28/has_one-vs-belongs_to/) is good; unfortunately the markdown format for links is different than the legacy Atlassian style so the closing `]` gets appended to the URL ‍♂️ – Sᴀᴍ Onᴇᴌᴀ Jun 22 '23 at 16:40

4 Answers4

90

The main difference is which side of the relation holds relationship's foreign key. The model that calls $this->belongsTo() is the owned model in one-to-one and many-to-one relationships and holds the key of the owning model.

Example one-to-one relationship:

class User extends Model {
  public function car() {
    // user has at maximum one car, 
    // so $user->car will return a single model
    return $this->hasOne('Car');
  }
}

class Car extends Model {
  public function owner() {
    // cars table has owner_id field that stores id of related user model
    return $this->belongsTo('User'); 
  }
}

Example one-to-many relationship:

class User extends Model {
  public function phoneNumbers() {
    // user can have multiple phone numbers, 
    // so $user->phoneNumbers will return a collection of models
    return $this->hasMany('PhoneNumber');
  }
}

class PhoneNumber extends Model {
  public function owner() {
    // phone_numbers table has owner_id field that stores id of related user model
    return $this->belongsTo('User'); 
  }
}
Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • 1
    An easier explanation can be found here : https://stackoverflow.com/questions/30058949/should-i-use-belongsto-or-hasone-in-laravel/30058999 – Istiaque Ahmed Jan 10 '20 at 15:29
  • That is so messed up IMO. If User has exactly one car, then user should have a reference to car_id. If car has a reference to user_id, then one user can be referenced by many cars - so user "has many" cars! Nonetheless, it is what it is. I have Appointment table and I thought if Appointment has maximum of 1 treatmentType, I wouldn't have guessed I have to use belongsTo. – Daniel Katz Dec 09 '20 at 01:16
  • @DanielKatz I agree the example of the relationship between User and Car is not the best. This example would be easier to understand if it was physically or legally impossible to own more than 1 car at the same time. – Robin Bastiaan Dec 09 '21 at 14:41
19

BelongsTo is a inverse of HasOne.

We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with User and Phone models.

I'm giving hasOne relation from User to Phone.

class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

Using this relation, I'm able to get Phone model data using User model.

But it is not possible with Inverse process using HasOne. Like Access User model using Phone model.

If I want to access User model using Phone, then it is necessary to add BelongsTo in Phone model.

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

You can refer this link for more detail.

Henk Poley
  • 729
  • 8
  • 17
Ketav
  • 760
  • 1
  • 8
  • 27
  • 6
    As I understand, from the perspective of the database, it makes no difference whether we will use `belongsTo` or `hasOne` or both. Let's imagine that we have two tables `A` and `B` and the `A` table has `b_id` foreign key to the `B` table. To implement this logic I can use either `A.belongsTo(B)` or `B.hasOne(A)` - any of these relations will create `b_id` on the `A` table. So, the main idea of using these two relations at once is to be able to populate our value from both models? Using `A` I will be able to populate `B` and vice-versa. Am I right? Thanks a lot. – Vladyslav Turak May 07 '18 at 11:45
4

One-to-one relationship: You, as a User, can have one (hasOne) Profile. And of course the inverse also applies. Profile (belongsTo) a User. A user can't have more than one profile and a profile can't belong to multiple users.

Naresh Ramoliya
  • 770
  • 2
  • 8
  • 25
3

If you want to make One TO one relationship between two table then first you have to make "hasOne" Relation and If you want to make inversely table relationship then you make " "Belongs to"... IT is a simple difference between HasOne and Belongs to the relationship if you want to know about this One To Many (Inverse)
Now that we can access all of a post's comments, let's define a relationship to allow a comment to access its parent post. To define the inverse of a hasMany relationship, define a relationship function on the child model which calls the belongsTo method:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Comment extends Model
{
    /**
     * Get the post that owns the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}
barbsan
  • 3,418
  • 11
  • 21
  • 28
Punit khandelwal
  • 129
  • 1
  • 13