简体   繁体   English

Laravel中BelongsTo和HasOne有什么区别

[英]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 . 雄辩属于关联HasOne关系。

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. 调用$this->belongsTo()的模型是one-to-onemany-to-one关系中的拥有模型,并拥有拥有模型的键。

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

BelongsTo is a inverse of HasOne. BelongsTo是HasOne的逆函数。

We can define the inverse of a hasOne relationship using the belongsTo method. 我们可以使用belongsTo方法定义hasOne关系的逆函数。 Take simple example with User and Phone models. 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. 使用此关系,我可以使用用户模型获取电话模型数据。

But it is not possible with Inverse process using HasOne . 但是使用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. 如果要使用Phone访问用户模型,则必须在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 link for more detail. 您可以参考此链接以获取更多详细信息。

One-to-one relationship: You, as a User, can have one ( hasOne ) Profile. 一对一关系:作为用户,您可以拥有一个( hasOne )配置文件。 And of course the inverse also applies. 当然,逆也适用。 Profile ( belongsTo ) a User. 为用户配置文件( belongsTo )。 A user can't have more than one profile and a profile can't belong to multiple users. 一个用户不能有多个配置文件,并且一个配置文件不能属于多个用户。

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) 如果要在两个表之间建立一对一关系,那么首先必须建立“ hasOne”关系,如果要使表反向,则要建立““属于” ......这是HasOne和如果您想了解一对多(反向),则属于该关系
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: 要定义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');
    }
}

Here you can see a good example and see what the difference is between BelongsTo and HasOne relationship in eloquent . 在这里你可以看到一个很好的例子,看看有什么区别雄辩 属于关联HasOne之间的关系。

Eloquent Relationships Cheat Sheet by Mahmoud Zalt https://link.medium.com/9lj9BAG8lR 雄辩的关系备忘单,Mahmoud Zalt https://link.medium.com/9lj9BAG8lR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM