laravel雄辩的关系hasmany查询 [英] laravel eloquent relationship hasmany query

查看:116
本文介绍了laravel雄辩的关系hasmany查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现 sql 查询

select * from user,comments where comments.user_id= user.id

所以我用以下代码在用户模型上创建了getcomments方法

so i create a getcomments method on my user model with following code

public function comments(){return $this->hasMany('Comments')}

现在正在通过

$data = User::find(1)->comments;

但是它只给我来自注释表的数据(而不是用户和注释) 我该怎么办

but it gave me the data only from comments table (not user and comments ) how can i do this

推荐答案

雄辩的ORM遵循活动记录模式.当您编写纯sql语句时,考虑建模和与数据进行交互的方式稍有不同.

The Eloquent ORM follows the Active Record pattern. It is a slightly different way to think about modeling and interacting with your data when you come from writing pure sql statements.

设置评论关系是一个好步骤.现在,您需要考虑如何与数据进行交互.

Setting up the comments relationship is a good step. Now you need to think about how you interact with your data.

您可以通过以下语句获取所有信息:

You can get all the information with the following statement:

$user = User::with('comments')->find(1);

使用此语句,所有用户信息都被加载到$user对象中,所有注释信息都被加载到$user->comments Collection属性中.可以像这样访问此信息:

With this statement, all of the user information is loaded into the $user object, and all of the comment information is loaded into the $user->comments Collection attribute. This information can be accessed like so:

// get the info
$user = User::with('comments')->find(1);

// display some user info
echo $user->first_name;
echo $user->last_name;

// loop through the comment Collection
foreach($user->comments as $comment) {
    // display some comment info
    echo $comment->text;
}

with('comments')部分告诉查询渴望为返回的用户加载所有评论(在这种情况下,仅是ID为1的评论).如果您不希望加载它们,则当您尝试访问它们时会自动将它们延迟加载.上面的代码在没有with('comments')的情况下将完全相同.但是,当您加载多个父记录(而不是仅一个)时,渴望加载变得更为重要,因为它解决了N + 1问题.您可以在此处急于加载.

The with('comments') section tells the query to eager load all the comments for the returned users (in this case, just the one with id 1). If you didn't eager load them, they would be lazy loaded automatically when you try to access them. The above code would work exactly the same without the with('comments'). Eager loading becomes more important when your loading multiple parent records, though, instead of just one, as it solves the N+1 problem. You can read about eager loading here.

警告(我添加新答案的原因):

User::find(1)->with('comments')->get();不会提供您要查找的信息.实际上,这最终将使您的所有用户都渴望加载其评论.这是原因:

User::find(1)->with('comments')->get();, as otherwise suggested, is not going to provide the information you're looking for. This will actually end up returning all your users with their comments eager loaded. Here is why:

首先,User::find(1)将返回ID为1的一个用户,这很好.但是,它随后在此模型上调用with('comments'),该模型实际上为users表创建了一个新的查询构建器实例.最后,它在这个新的查询生成器实例上调用get(),并且由于它没有任何约束,它将返回表中的所有用户,并附加所有渴望加载的用户注释.

First, User::find(1) is going to return the one user with an id of 1, which is good. However, it then calls with('comments') on this model, which actually creates a new query builder instance for the users table. Finally, it calls get() on this new query builder instance, and since it doesn't have any constraints on it, it will return all the users in the table, with all the comments attached to those users eager loaded.

这篇关于laravel雄辩的关系hasmany查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆