Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between BelongsTo And HasOne in Laravel

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

like image 327
Pardeep Pathania Avatar asked Jun 02 '16 04:06

Pardeep Pathania


People also ask

What is belongsTo in laravel?

BelongsTo relationship in laravel is used to create the relation between two tables. belongsTo means create the relation one to one in inverse direction or its opposite of hasOne. For example if a user has a profile and we wanted to get profile with the user details then we can use belongsTo relationship.

What is hasOne laravel?

hasOne relationship in laravel is used to create the relation between two tables. hasOne means create the relation one to one. For example if a article has comments and we wanted to get one comment with the article details then we can use hasOne relationship or a user can have a profile table.

What is the difference between hasMany and belongsTo?

belongsTo(B) association means that a One-To-One relationship exists between A and B , with the foreign key being defined in the source model ( A ). The A. hasMany(B) association means that a One-To-Many relationship exists between A and B , with the foreign key being defined in the target model ( B ).

What is belongsTo?

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.


3 Answers

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'); 
  }
}
like image 91
jedrzej.kurylo Avatar answered Oct 09 '22 07:10

jedrzej.kurylo


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.

like image 22
Ketav Avatar answered Oct 09 '22 05:10

Ketav


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.

like image 4
Naresh Ramoliya Avatar answered Oct 09 '22 06:10

Naresh Ramoliya