<bdo id='HWeGb'></bdo><ul id='HWeGb'></ul>

  • <i id='HWeGb'><tr id='HWeGb'><dt id='HWeGb'><q id='HWeGb'><span id='HWeGb'><b id='HWeGb'><form id='HWeGb'><ins id='HWeGb'></ins><ul id='HWeGb'></ul><sub id='HWeGb'></sub></form><legend id='HWeGb'></legend><bdo id='HWeGb'><pre id='HWeGb'><center id='HWeGb'></center></pre></bdo></b><th id='HWeGb'></th></span></q></dt></tr></i><div id='HWeGb'><tfoot id='HWeGb'></tfoot><dl id='HWeGb'><fieldset id='HWeGb'></fieldset></dl></div>

        <legend id='HWeGb'><style id='HWeGb'><dir id='HWeGb'><q id='HWeGb'></q></dir></style></legend>
        <tfoot id='HWeGb'></tfoot>
      1. <small id='HWeGb'></small><noframes id='HWeGb'>

        Order By before Group By 使用 Eloquent (Laravel)

        时间:2023-09-24
      2. <small id='enVxs'></small><noframes id='enVxs'>

          <bdo id='enVxs'></bdo><ul id='enVxs'></ul>
          <tfoot id='enVxs'></tfoot>

          <i id='enVxs'><tr id='enVxs'><dt id='enVxs'><q id='enVxs'><span id='enVxs'><b id='enVxs'><form id='enVxs'><ins id='enVxs'></ins><ul id='enVxs'></ul><sub id='enVxs'></sub></form><legend id='enVxs'></legend><bdo id='enVxs'><pre id='enVxs'><center id='enVxs'></center></pre></bdo></b><th id='enVxs'></th></span></q></dt></tr></i><div id='enVxs'><tfoot id='enVxs'></tfoot><dl id='enVxs'><fieldset id='enVxs'></fieldset></dl></div>
                <tbody id='enVxs'></tbody>
              <legend id='enVxs'><style id='enVxs'><dir id='enVxs'><q id='enVxs'></q></dir></style></legend>

                  本文介绍了Order By before Group By 使用 Eloquent (Laravel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个包含以下列的消息"表

                  I have a "messages" table with the following columns

                  CREATE TABLE `messages` (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `fromId` int(11) NOT NULL,
                    `toId` int(11) NOT NULL,
                    `message` text NOT NULL,
                    `status` int(11) NOT NULL,
                    `device` varchar(100) NOT NULL,
                    `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                    PRIMARY KEY (`id`)
                  ) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=latin1;
                  

                  我正在尝试获取所有 'toId' = $id 并按 fromId 分组的消息.问题是结果中显示的消息"是第一个,而不是最新的.我尝试通过 createdAt 订购,但它不起作用.

                  I'm trying to get all messages where 'toId' = $id and grouping by fromId. The problem is that the "message" shown on the results is the first ones, not the latest ones. I tried ordering by createdAt but it's not working.

                  如何在查询和分组结果之前按createdAt"排序?我想使用 Eloquent 以 laravel 的方式做到这一点.

                  How can I order by "createdAt" prior to querying and grouping the results? I want to do this in the laravel way using Eloquent.

                  我的查询:

                  $chats = Message::with('sender','recipient')
                          ->where('toId',$id)
                          ->orderBy('createdAt')
                          ->groupBy('fromId')
                          ->paginate(10)
                  

                  推荐答案

                  我找到了一种方法!基本上,创建一个子查询并在之前运行它,以便结果按预期排序并在其后分组.

                  I found a way to do that! Basically, creating a subquery and running it before, so that results are ordered as expected and grouped after.

                  代码如下:

                  $sub = Message::orderBy('createdAt','DESC');
                  
                  $chats = DB::table(DB::raw("({$sub->toSql()}) as sub"))
                      ->where('toId',$id)
                      ->groupBy('fromId')
                      ->get();
                  

                  这篇关于Order By before Group By 使用 Eloquent (Laravel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Eloquent 多对多对多 - 如何轻松加载远距离关系 下一篇:Laravel 4.1:如何对雄辩的渴望关系进行分页?

                  相关文章

                  最新文章

                  <tfoot id='ucgcB'></tfoot>
                    • <bdo id='ucgcB'></bdo><ul id='ucgcB'></ul>
                    <i id='ucgcB'><tr id='ucgcB'><dt id='ucgcB'><q id='ucgcB'><span id='ucgcB'><b id='ucgcB'><form id='ucgcB'><ins id='ucgcB'></ins><ul id='ucgcB'></ul><sub id='ucgcB'></sub></form><legend id='ucgcB'></legend><bdo id='ucgcB'><pre id='ucgcB'><center id='ucgcB'></center></pre></bdo></b><th id='ucgcB'></th></span></q></dt></tr></i><div id='ucgcB'><tfoot id='ucgcB'></tfoot><dl id='ucgcB'><fieldset id='ucgcB'></fieldset></dl></div>

                      <small id='ucgcB'></small><noframes id='ucgcB'>

                    1. <legend id='ucgcB'><style id='ucgcB'><dir id='ucgcB'><q id='ucgcB'></q></dir></style></legend>