<legend id='iBhL8'><style id='iBhL8'><dir id='iBhL8'><q id='iBhL8'></q></dir></style></legend>

      <i id='iBhL8'><tr id='iBhL8'><dt id='iBhL8'><q id='iBhL8'><span id='iBhL8'><b id='iBhL8'><form id='iBhL8'><ins id='iBhL8'></ins><ul id='iBhL8'></ul><sub id='iBhL8'></sub></form><legend id='iBhL8'></legend><bdo id='iBhL8'><pre id='iBhL8'><center id='iBhL8'></center></pre></bdo></b><th id='iBhL8'></th></span></q></dt></tr></i><div id='iBhL8'><tfoot id='iBhL8'></tfoot><dl id='iBhL8'><fieldset id='iBhL8'></fieldset></dl></div>
      1. <tfoot id='iBhL8'></tfoot>
      2. <small id='iBhL8'></small><noframes id='iBhL8'>

        • <bdo id='iBhL8'></bdo><ul id='iBhL8'></ul>

        按多列对 Eloquent 集合进行排序的语法是什么?

        时间:2023-09-24

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

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

              <legend id='fOzib'><style id='fOzib'><dir id='fOzib'><q id='fOzib'></q></dir></style></legend>

                <tbody id='fOzib'></tbody>
                • 本文介绍了按多列对 Eloquent 集合进行排序的语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我知道在使用查询构建器时,可以使用

                  I know that when using the query builder, it is possible to sort by multiple columns using

                  ...orderBy('column1')->orderBy('column2')
                  

                  但现在我正在处理一个 collection 对象.集合具有 sortBy 方法,但我无法弄清楚如何使其适用于多列.直觉上,我最初尝试使用与 orderBy 相同的语法.

                  but now I am dealing with a collection object. Collections have the sortBy method, but I have not been able to figure out how to make it work for multiple columns. Intuitively, I initially tried to use the same syntax as orderBy.

                  sortBy('column1')->sortBy('column2)
                  

                  但这显然只是按顺序应用排序,最终按 column2 排序,而忽略 column1.我试过了

                  but this apparently just applies the sorts sequentially and it ends up sorted by column2, disregarding column1. I tried

                  sortBy('column1', 'column2')
                  

                  但这会引发错误asort() 期望参数 2 很长,给出的字符串".使用

                  but that throws the error "asort() expects parameter 2 to be long, string given". Using

                  sortBy('column1, column2')
                  

                  不会抛出错误,但排序似乎非常随机,所以我真的不知道它实际上做了什么.我查看了 sortBy 方法的代码,但不幸的是我很难理解它是如何工作的.

                  doesn't throw an error, but the sort appears to be pretty random, so I don't really know what that actually does. I looked at the code for the sortBy method, but unfortunately I am having a hard time understanding how it works.

                  推荐答案

                  sortBy() 接受一个闭包,允许您提供一个应该用于排序比较的值,但您可以使它是通过将几个属性连接在一起而形成的组合

                  sortBy() takes a closure, allowing you to provide a single value that should be used for sorting comparisons, but you can make it a composite by concatenating several properties together

                  $posts = $posts->sortBy(function($post) {
                      return sprintf('%-12s%s', $post->column1, $post->column2);
                  });
                  

                  如果您需要对多列进行 sortBy,您可能需要对它们进行空格填充以确保ABC"和DEF"出现在AB"和DEF"之后,因此冲刺右填充每列直到列的长度(至少对于除最后一列之外的所有列)

                  If you need the sortBy against multiple columns, you probably need to space pad them to ensure that "ABC" and "DEF" comes after "AB" and "DEF", hence the sprint right padded for each column up to the column's length (at least for all but the last column)

                  请注意,如果您可以在查询中使用 orderBy,那么通常会更有效率,以便在从数据库检索时准备好对集合进行排序

                  Note that it's generally a lot more efficient if you can use an orderBy in your query so the collection is ready-sorted on retrieval from the database

                  这篇关于按多列对 Eloquent 集合进行排序的语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Eloquent 中为列的名称设置别名 下一篇:如何为 Laravel/Eloquent 模型设置默认属性值?

                  相关文章

                  最新文章

                • <tfoot id='TWbBc'></tfoot>

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

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