Laravel eloquent - 一对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12941397/
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
Laravel eloquent - One to many relationships
提问by Allister
I have just got started with laravel v3 and am trying to wrap my head around eloquent's One-To-Many relationships by creating a blog, I have posts that have a many to one relationship with categories (Each Post is linked to a category).
我刚刚开始使用 laravel v3,并试图通过创建博客来解决 eloquent 的一对多关系,我的帖子与类别具有多对一的关系(每个帖子都链接到一个类别)。
I have the following tables with the following fields:
我有以下表格,其中包含以下字段:
posts: id, title, body, date_created, category_id
帖子:id、标题、正文、date_created、category_id
categories: id, name
类别:ID,名称
I have the following two models:
我有以下两个模型:
class Category extends Eloquent
{
public function posts()
{
return $this->has_many('Post');
}
}
class Post extends Eloquent
{
public function categories()
{
return $this->belongs_to('Category');
}
}
I figured out how to get all posts by passing in a category id:
我想出了如何通过传入类别 ID 来获取所有帖子:
category::find(2)->posts()->get())
I just need help on finding out how to get all posts, and get their corrisponding categories. So at the end of the day in the view I can output something like this:
我只需要帮助找出如何获取所有帖子,并获取相应的类别。所以在一天结束时,我可以输出如下内容:
{$post->title} - Category: {$post->category->name}
Thanks for any help!
谢谢你的帮助!
回答by William Cahill-Manley
I hope you will find these tips useful.
我希望你会发现这些技巧很有用。
On the post model rename the categories
function to category
. A belongs_to
relation is singular, so this post has one and only one category.
在 post 模型上,将categories
函数重命名为category
. 一个belongs_to
关系是单数,所以这个帖子有且只有一个类别。
Relations also have a short hand, this shorthand syntax is useful because it is cleaner to use and the results are cached. Heres an example:
关系也有简写,这种简写语法很有用,因为它使用起来更干净,结果也被缓存了。这是一个例子:
$category = Category::find(1);
foreach($category->posts as post) {
echo $post->title;
}
Now an example of how to get all the posts with their related categories:
现在是如何获取所有具有相关类别的帖子的示例:
$posts = Post::all();
foreach($posts as $post) {
echo $post->category->name;
}
Now one thing you will quickly notice when doing this second example is the number of queries increase for each post you have. This is called the N+1 effect. Example, if you have 5 posts, one query will be executed to get those posts. Then in the loop we are executing a query to get the category. This results in 6 total queries.
现在,在执行第二个示例时,您会很快注意到的一件事是,您拥有的每篇文章的查询数量都会增加。这称为 N+1 效应。例如,如果您有 5 个帖子,将执行一个查询来获取这些帖子。然后在循环中我们执行查询以获取类别。这导致总共 6 个查询。
To solve this problem, use eager loading, which reduces those 6 queries in our example down to 2.
为了解决这个问题,使用预先加载,这将我们示例中的这 6 个查询减少到 2 个。
$posts = Post::with('category')->all();
foreach($posts as $post) {
echo $post->category->name;
}
I hope that helps!
我希望这有帮助!
回答by JohnD
Laravel 4 has a slightly different syntax , it uses camelCase to build expressions (while L3 uses snake_Case syntax).Laravel's (L4) new syntax is now PSR-1 compliant.
Laravel 4 的语法略有不同,它使用驼峰命名法来构建表达式(而 L3 使用 snake_Case 语法)。Laravel (L4) 的新语法现在符合 PSR-1。
L3 : $this->belongs_to('Category');
L4 : $this->belongsTo('Category');
To prove that "eager loading" boosts the performance of your application (by minimizing database queries) , use Laravel's event eco-system .
为了证明“急切加载”提高了应用程序的性能(通过最小化数据库查询),请使用 Laravel 的事件生态系统。
// app/routes.php
Event::listen('illuminate.query', function($sql) { echo '<h4>' . $sql . '</h4>' ;});
.
.