阐明如何在 Laravel 的 Eloquent ORM 中建立一对多关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16869249/
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 07:51:12  来源:igfitidea点击:

Clarify how to setup a one-to-many relationship in Laravel's Eloquent ORM

phpormlaravellaravel-4eloquent

提问by eimajenthat

Okay, I'm working through the Laravel 4 docs to setup a one-to-many relationship between two models. Obviously, one side should use hasMany(). But for the other side, should I use hasOne or belongsTo? Does it matter? What's difference? Why do both exist?

好的,我正在研究 Laravel 4 文档以在两个模型之间建立一对多关系。显然,一侧应该使用 hasMany()。但另一方面,我应该使用hasOne还是belongsTo?有关系吗?有什么区别?为什么两者都存在?

I had thought hasOne would be for one-to-one relationships, and belongsTo would be for the one side of one-to-many. But in the docs, for inserting a related model here:

我原以为 hasOne 将用于一对一关系,而belongsTo 将用于一对多的一侧。但是在文档中,用于在此处插入相关模型:

http://laravel.com/docs/eloquent#inserting-related-models

http://laravel.com/docs/eloquent#inserting-related-models

they are using save()which seems to only be present in hasOneand hasManyrelationships, not in belongsTo. It looks like belongsTouses associate()for the same purpose:

他们正在使用save()which 似乎只存在于hasOnehasMany关系中,而不存在于belongsTo. 看起来belongsTo用途associate()相同:

https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php#L188

https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php#L188

Maybe I just need some general background on when to use belongsTovs. hasOne, and why belongsTouses associate()while hasOneuses save().

也许我只需要一些关于何时使用belongsTovs. 的一般背景hasOne,以及为什么belongsTo使用associate()whilehasOne使用save().

EDIT: I guess my point of confusion was that in the docs (http://laravel.com/docs/eloquent#inserting-related-models), they used:

编辑:我想我的困惑点是在文档(http://laravel.com/docs/eloquent#inserting-related-models)中,他们使用了:

$post->comments()->save($comment);

where I would have used:

我会在哪里使用:

$comment->post()->associate($post);

Is there an advantage to one way or the other? Or is it just a question of what makes sense in the context?

一种方式或另一种方式有优势吗?或者这只是一个在上下文中有意义的问题?

采纳答案by Thomas Clarkson

Please refer to the laravel docs on the one to many relationship between posts and comments http://laravel.com/docs/eloquent#relationships. (Where one post has many comments and a comment belongs to a post).

请参阅有关帖子和评论之间一对多关系的 laravel 文档http://laravel.com/docs/eloquent#relationships。(一个帖子有很多评论,一个评论属于一个帖子)。

The tables for posts and comments be as follows

帖子和评论表如下

Posts Tableid | title | body

帖子表id | 标题 | 身体

Comments Tableid | comment | post_id

评论表ID | 评论 | post_id

The database table that contains a foreign key belongs to a record in the other table, therefore, in the comments model you must specify that comments belongs to posts.

包含外键的数据库表属于另一个表中的记录,因此,在评论模型中,您必须指定评论属于帖子。

You are correct that the hasOne relationship only applies to one to one relationships.

您是正确的, hasOne 关系仅适用于一对一关系。

Here is a blog post with laravel 3 code which gives an explanation into how eloquent relationship methods work.

这是一篇包含 Laravel 3 代码的博客文章,它解释了 eloquent关系方法的工作原理。

http://laravel.io/topic/14/how-eloquent-relationship-methods-work

http://laravel.io/topic/14/how-eloquent-relationship-methods-work