简体   繁体   English

了解Laravel中的hasOne()和belongsTo()函数

[英]Understanding hasOne() and belongsTo() functions in Laravel

All I'm trying to do is understanding when exactly should I use hasOne() and when should I use belongsTo() . 我所要做的就是了解我应该何时使用hasOne()以及何时应该使用belongsTo() Both seem identical to me. 两者看起来都和我一样。 For example, here is my model: 例如,这是我的模型:

use Illuminate\Database\Eloquent\Model;
use App\Categories;
use App\User;


class tickets extends Model
{
    protected $table = "tickets";

    public function category()
    {
        return $this->hasOne(Categories::class, 'id', 'category_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
}

I can do the same by using belongsTo() function too. 我也可以通过使用belongsTo()函数来做同样的事情。 Just I should put them into user and category models instead. 我应该将它们放入usercategory模型中。 Anyway, when should I use either hasOne() or belongsTo() ? 无论如何,我什么时候应该使用hasOne()belongsTo()

When dealing with 1 to many relationships, you will have a hasMany() and a belongsTo() . 处理1对多关系时,您将拥有hasMany()belongsTo()

The rule of thumb I use, is if a table has a foreign key (tickets table has a user_id fk) then this model belongsTo users. 我使用的经验法则是,如果一个表有一个外键(票证表有一个user_id fk),那么这个模型belongsTo用户。 The same with Category. 与分类相同。

So your above example, Ticket belongsTo User & Category . 所以上面的例子, Ticket belongsTo UserCategory

Inversely, User hasMany Ticket and similarly Category hasMany Ticket 相反, User hasMany Ticket和类似的Category hasMany Ticket

Anyway, when should I use either hasOne() or belongsTo() ? 无论如何,我什么时候应该使用hasOne()或belongsTo()?

Think of it like what would be hasMany if there is one to many relation probable in the future. 认为它像什么是hasMany如果有一对多的关系在未来可能的。 For example: consider User and Phone models. 例如:考虑用户和手机型号。 Nature of relation would be User hasOne Phone, but if you would like to extend functionality for it so user could have multiple phone numbers registered User would still have has* relation while Phone would keep belongsTo relation. 关系的性质将是User hasOne Phone,但如果您想扩展其功能,那么用户可以注册多个电话号码用户仍然has*关系,而Phone将保持belongsTo关系。 Just consider which one could be "parent" entity and that one should have hasOne relation in method. 只考虑哪一个可以是“父”实体,并且一个应该在方法中具有hasOne关系。 I would always consider User as parent entity and for me logically would be user has one ticket. 我总是将User视为父实体,对我来说逻辑上是用户有一张票。 Also, try to stick with Eloquent/Laravel/artisan naming convention and name that model Ticket and other one Category (Eloquent and Laravel will solve plural where needed ie table name). 此外,尝试坚持使用Eloquent / Laravel / artisan命名约定并命名模型Ticket和其他一个Category (Eloquent和Laravel将在需要时解析复数,即表名)。

These are same with a single difference. 这些是相同的,只有一个区别。 Both returns the single associated object with one difference. 两者都返回单个关联对象,但有一个区别。 When we declare some relation as belongsTo it means there is a database table which has a foreign key of some other table. 当我们将某个关系声明为belongsTo时,它意味着有一个数据库表,其中包含某个其他表的外键。 When we declare hasOne relation it means that this table's primary key has been referenced in another table. 当我们声明hasOne关系时,它意味着该表的主键已在另一个表中引用。 Think of it as a parent child table. 将其视为父子表。 When we would make the child table we reference each child to its parent, right? 当我们制作子表时,我们将每个孩子引用给它的父母,对吗? This is belongsTo . 这是属于 And when we would make the parent table we know that each entry in parents table can have a single or many entries in the child table. 当我们创建父表时,我们知道父表中的每个条目在子表中都可以有一个或多个条目。 That's hasOne or hasMany relation. 这是hasOne的hasMany关系。 You can ask further if you need any more clarification. 如果您需要进一步澄清,可以进一步询问。

hasOne is a 1:1, or one-to-one relationship. hasOne是1:1或一对一的关系。

hasMany is a 1:n, or one-to-many relationship. hasMany是1:n或一对多关系。

The belongsTo method in Eloquent is used to define the inverse of these relationships. belongsTo中的belongsTo方法用于定义这些关系的反转

The definition of these will depend on your data model. 这些的定义取决于您的数据模型。

In your case: 在你的情况下:

You have a Category model, which hasMany Ticket s. 你有一个Category模型,它有许多hasMany Ticket

You also have a User model, which hasMany Ticket s. 您还有一个User模型,其中包含许多hasMany Ticket

Now from the Ticket perspective, you would want to define the inverses of these 2 hasMany relationships. 现在从Ticket角度来看,您需要定义这两个hasMany关系的反转。 You will do this by defining a belongsTo . 您将通过定义belongsTo来完成此belongsTo

So the Ticket belongsTo a User and belongsTo a Category . 因此Ticket belongsTo一个User并且belongsTo一个Category


To answer your question: 回答你的问题:

From the Ticket s perspective, it is a 1:1 relation, because the foreign key in the Ticket model points to 1 User and the category foreign key points to 1 Category. Ticket的角度来看,它一个1:1的关系,因为Ticket模型中的外键指向1 User,类别外键指向1 Category。

But since the relation you created is a 1:n (one-to-many) and you have also defined it on the User and Category models, you should define the relation in your Ticket model as the inverse of those relations, and the inverse of a hasMany (and hasOne) is belongsTo . 但是,由于您所创建的关系是1:N(一到多),你还定义它的用户和类别车型,你应该定义在您的机票模型这些关系的的关系,以及逆一个hasMany(和hasOne)是belongsTo

When defining your relations in Laravel, keep your database schema in mind and define your relations in the same way that they exist in your database schema. 在Laravel中定义关系时,请记住数据库模式,并以与数据库模式中存在的关系相同的方式定义关系。

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

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