简体   繁体   English

laravel雄辩的关系hasmany查询

[英]laravel eloquent relationship hasmany query

i am trying to implement a sql query 我正在尝试实现sql查询

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

so i create a getcomments method on my user model with following code 所以我用以下代码在用户模型上创建了一个getcomments方法

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

and now am accessing the data by 现在正在通过访问数据

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

but it gave me the data only from comments table (not user and comments ) how can i do this 但是它只给我注释表中的数据(而不是用户和注释),我该怎么做

The Eloquent ORM follows the Active Record pattern. 雄辩的ORM遵循活动记录模式。 It is a slightly different way to think about modeling and interacting with your data when you come from writing pure sql statements. 当您来自编写纯sql语句时,这是考虑建模和与数据进行交互的方式稍有不同。

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);

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. 使用此语句,所有用户信息都被加载到$user对象中,所有注释信息都被加载到$user->comments Collection属性中。 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;
}

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). with('comments')部分告诉查询渴望为返回的用户加载所有评论(在这种情况下,仅是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') . 如果没有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. 但是,当您加载多个父记录而不是仅一个时,渴望加载就变得更加重要,因为它解决了N + 1问题。 You can read about eager loading here . 您可以在此处阅读有关急切加载的信息

Caution (the reason I added a new answer): 警告(我添加新答案的原因):

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: 原因如下:

First, User::find(1) is going to return the one user with an id of 1, which is good. 首先, User::find(1)将返回一个ID为1的用户,这很好。 However, it then calls with('comments') on this model, which actually creates a new query builder instance for the users table. 但是,它随后在此模型上调用with('comments') ,这实际上为users表创建了一个新的查询构建器实例。 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. 最后,它在这个新的查询生成器实例上调用get() ,并且由于它没有任何约束,它将返回表中的所有用户,并附加所有渴望加载的用户注释。

First find a user. 首先找到一个用户。 Then access all user data, and all comments of user. 然后访问所有用户数据以及用户的所有注释。

$data = User::find(1);
//or you can use eager loading for more performance. thanks for @Özgür Adem Işıklı
$data = User::with('comments')->find(1);

//access user data
$data->id;
$data->email; //etc.

//user's comments:
foreach($data->comments as $comment) {
    //access comment detail
    $comment->id;
    $comment->title;
}

You are fetching just comments of the user which you select by id. 您仅获取按ID选择的用户的评论。 You should this; 你应该这样;

User::find(1)->with('comments')->get();

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

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