具有来自多个表的属性的 Laravel 4 模型

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

Laravel 4 model with attributes from multiple tables

sqljoinormlaravellaravel-4

提问by matt

I would like to have a model that belongs to another one. In my controller, I'd like to get all items in that model, but I want the attributes from the table it belongs to as well. For example:

我想要一个属于另一个模型的模型。在我的控制器中,我想获取该模型中的所有项目,但我也想要它所属的表中的属性。例如:

class Comment extends Eloquent {

    public function post()
    {
        return $this->belongsTo('Post');
    }

}

In my controller I can do the following to get the comment data:

在我的控制器中,我可以执行以下操作来获取评论数据:

$comments = Comment::first();

However, that will only give me data from the comments table (and no joined data from the posts table). I would like the data from the posts table for the row that each comment belongs to available as attributes to my Comment model. I know that I can also do the following to get the data from the posts table:

但是,这只会给我来自评论表的数据(并且没有来自帖子表的连接数据)。我希望每个评论所属行的帖子表中的数据可用作我的评论模型的属性。我知道我还可以执行以下操作来从帖子表中获取数据:

$comments = Comment::first();

$comments = Comment::first();

The issue with doing it this way is that it uses two database queries (#1 to get the comment, and #2 to get the post data that it belongs to). Is there a way that I get the data from both tables into my model, equivalent to a join statement:

这样做的问题在于它使用两个数据库查询(#1 获取评论,#2 获取它所属的帖子数据)。有没有办法将两个表中的数据都放入我的模型中,相当于连接语句:

SELECT * FROM comments LEFT JOIN posts ON comments.post_id = posts.id

I Know that I build a join query manually without using my Comment or Post models, but I have several methods in my Comment model that I'd like to be able to use with the data that is retrieved. Anyone know how I can accomplish this?

我知道我在不使用 Comment 或 Post 模型的情况下手动构建了一个连接查询,但是我的 Comment 模型中有几个方法我希望能够与检索到的数据一起使用。有谁知道我怎么能做到这一点?

回答by fideloper

From the documentation on eager loading:

关于预先加载文档

Thankfully, we can use eager loading to drastically reduce the number of queries. The relationships that should be eager loaded may be specified via the with method [...]

值得庆幸的是,我们可以使用预先加载来大幅减少查询数量。应该预先加载的关系可以通过 with 方法 [...]

Using the with()parameter will use a constant number? of queries in a one-to-many relationship. Therefore, this is query should retrieve your comments with their related post in one query:

使用with()参数将使用一个常数?一对多关系中的查询。因此,此查询应在一个查询中检索您的评论及其相关帖子:

$comments = Comments::with('post')->get();

? A Constant number of queries as opposed to linearly increasing in query count per number of comments

? 恒定数量的查询,而不是每条评论的查询数量线性增加

回答by Arda

If you want to get the results only with one query done on the background, without using with(), you can use fluentand join().

如果您只想通过在后台完成一个查询来获得结果,而不使用with(),则可以使用fluentjoin()

SELECT * FROM comments LEFT JOIN posts ON comments.post_id = posts.id

is equal to:

等于:

DB::table('comments')
    ->join('posts','comments.post_id','=','posts.id','left')
    ->get();

But I think also adding a groupBy()statement will give you better results:

但我认为添加一个groupBy()语句会给你更好的结果:

Example:

例子:

DB::table('comments')
    ->join('posts','comments.post_id','=','posts.id','left')
    ->groupBy('comments.id')
    ->get();

But I'd prefer to use the other answer in my projects:

但我更愿意在我的项目中使用其他答案:

Comment::with('post')->get();

回答by kaning

$comments:: all()->with ('post')->get()

$comments:: all()->with ('post')->get()