Laravel 中的 BelongsTo 和 HasOne 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37582848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:55:36  来源:igfitidea点击:

What is the difference between BelongsTo And HasOne in Laravel

phplaravellaravel-5eloquent

提问by Pardeep Pathania

Can any body tell me what is the main difference between
the BelongsToand HasOnerelationship in eloquent.

任何机构可以告诉我是什么样的区别主要
属于关联HasOne的关系雄辩

采纳答案by Ketav Chotaliya

BelongsTo is a inverse of HasOne.

BelongsTo 是 HasOne 的反函数。

We can define the inverse of a hasOne relationship using the belongsTo method. Take simple example with Userand Phonemodels.

我们可以使用belongsTo 方法定义hasOne 关系的逆。举个简单的例子UserPhone模型。

I'm giving hasOne relation from User to Phone.

我给了从用户到电话的 hasOne 关系。

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.

使用这种关系,我可以使用 User 模型获取 Phone 模型数据。

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

但是使用HasOne 的逆过程是不可能的。就像使用电话模型访问用户模型一样。

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

如果我想使用Phone 访问User 模型,则需要在Phone 模型中添加BelongsTo

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

You can refer this linkfor more detail.

您可以参考此链接了解更多详情。

回答by jedrzej.kurylo

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-oneand many-to-onerelationships and holds the key of the owning model.

主要区别在于关系的哪一边持有关系的外键。调用$this->belongsTo()的模型是one-to-onemany-to-one关系中的拥有模型,并持有拥有模型的键。

Example one-to-onerelationship:

一对一关系示例:

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-manyrelationship:

一对多关系示例:

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

回答by Naresh Ramoliya

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.

一对一关系:作为用户,您可以拥有一个 ( hasOne) 个人资料。当然,反过来也适用。个人资料(belongsTo)一个用户。一个用户不能拥有多个配置文件,一个配置文件不能属于多个用户。

回答by Punit khandelwal

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 belongsTomethod:

如果你想在两个表之间建立一对一的关系,那么首先你必须建立“hasOne”关系,如果你想建立相反的表关系,那么你就建立“属于”......这是 HasOne 和属于关系如果你想知道这个一对多(逆)
现在我们可以访问一个帖子的所有评论,让我们定义一个关系来允许一个评论访问它的父帖子。定义一个 hasMany 关系的逆,在调用该belongsTo方法的子模型上定义一个关系函数:

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

回答by Mihail Kuznetsov

Here you can see a good example and see what the difference is between BelongsToand HasOnerelationship in eloquent.

在这里你可以看到一个很好的例子,看看有什么区别之间属于关联HasOne的关系雄辩

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

Mahmoud Zalt 的雄辩关系备忘单https://link.medium.com/9lj9BAG8lR