简体   繁体   English

通过查询Laravel 5.1进行分组

[英]GroupBy Query Laravel 5.1

I'm having problem in fetching the data using groupBy, I don't where I'm wrong, I have done it many times before, but today I'm wrong some where and I don't know where. 我在使用groupBy提取数据时遇到问题,我没有记错,我之前做过很多次,但是今天我在某些地方记错了,我不知道在哪里。 Following is the Table from which I want to select the Data: 以下是我要从中选择数据的表:

Table Name: user_questions 表名称: user_questions

id | user_id | message | read_status_user | read_status_support | answered

Now suppose if one user sends more than one messages, then user_id will be repeated, So to want all the message from one particular user I'm firing the query like following: 现在假设如果一个用户发送了多个消息,则将重复user_id,因此,想要来自一个特定用户的所有消息,我将触发查询,如下所示:

UserQuestion::groupBy('user_id')->get();

This should give me the result like 这应该给我结果

user_id = 1 > message1
user_id = 1 > message2
....
user_id = 1 > message...(if any)

user_id = 2 > message1
user_id = 2 > message2
..... 
So on...

But this is always giving me only one message from the particular user. 但这总是只给我来自特定用户的一条消息。 I don't know why. 我不知道为什么 Is there any mistake? 有没有错 I have tried another queries too, but all are giving me the same result. 我也尝试了另一个查询,但是所有查询都给我相同的结果。

Please help me with this. 请帮我解决一下这个。 Everybody's help will be highly appreciated. 非常感谢大家的帮助。 Thanks to all of you in advance. 预先感谢大家。

The issue here is that you are calling the groupBy function of the query builder object, which is what generates the query for your database. 这里的问题是您正在调用查询构建器对象的groupBy函数,该函数将为您的数据库生成查询。 When you call the ->get() method, the query is executed and a Collection object containing the results is returned. 当您调用->get()方法时,将执行查询并返回包含结果的Collection对象。 What you are looking to use is the groupBy method of Laravel's Collection class, which means you need to put the ->groupBy('user_id') after the ->get() . 您要使用的是Laravel的Collection类的groupBy方法,这意味着您需要将->groupBy('user_id')放在->get()

Assuming you have the following data: 假设您具有以下数据:

user_question
user_id     question_id
1           1
1           2
1           3
2           4
3           5
3           6

Your current code 您当前的代码

UserQuestion::groupBy('user_id')->get();

executes this query 执行此查询

select * from user_question group by user_id;

returning one row per user, since that's what group by does in MySQL. 每个用户返回一行,因为group by在MySQL中就是这样做的。

user_id     question_id
1           1
2           4
3           5

If instead, you do the following 如果相反,请执行以下操作

$collection = UserQuestion::get();

the query is simply 查询很简单

select * from user_question

and when you call $collection->groupBy('user_id') on this collection, you get data structured like 当您在此集合上调用$collection->groupBy('user_id')时,您得到的数据结构如下

[
     1 => [
         [ 'user_id' => 1, 'question_id' => 1 ],
         [ 'user_id' => 1, 'question_id' => 2 ],
         [ 'user_id' => 1, 'question_id' => 3 ]
      ],
      2 => [
          [ 'user_id' => 2, 'question_id' => 4 ],
      ],
      3 => [
          [ 'user_id' => 3, 'question_id' => 5 ],
          [ 'user_id' => 3, 'question_id' => 6 ]
      ]
]

Try like this 这样尝试

$users = DB::table('table_name')
        ->groupBy('user_id')
        ->get();

after that push that to foreach loop 之后将其推送到foreach循环

foreach ($users as $user)
{
    var_dump($user->name);
}

ordering-grouping-limit-and-offset in Laravel Laravel中的排序分组限制和偏移

You've probably found the solution to your problem by now but otherwise, I would suggest to use the relationships. 到现在您可能已经找到了解决问题的方法,但否则,我建议您使用这些关系。 In the User model, I would do: 在用户模型中,我将执行以下操作:

public function questions()
{
    return $this->hasMany('App\UserQuestion');
}

Then I would get all the users and loop through them to get their messages. 然后,我将吸引所有用户并遍历他们以获取他们的消息。

$users = User::all();
$users->each(function ($user) {
    $questions = User::find($user->id)->questions;
});

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

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