What is the difference between BelongsTo And HasOne in Laravel

PhpLaravelLaravel 5Eloquent

Php Problem Overview


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

Php Solutions


Solution 1 - Php

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'); 
  }
}

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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');
    }
}

Solution 5 - Php

Here you can see a good example and see what the difference is between BelongsTo and HasOne relationship in eloquent.

Eloquent Relationships Cheat Sheet by Mahmoud Zalt https://link.medium.com/9lj9BAG8lR

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
QuestionPardeep PathaniaView Question on Stackoverflow
Solution 1 - Phpjedrzej.kuryloView Answer on Stackoverflow
Solution 2 - PhpKetavView Answer on Stackoverflow
Solution 3 - PhpNaresh RamoliyaView Answer on Stackoverflow
Solution 4 - PhpPunit khandelwalView Answer on Stackoverflow
Solution 5 - PhpMihail KuznetsovView Answer on Stackoverflow