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

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

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

      <tfoot id='UbbET'></tfoot>

      按 created_at 排序 Eloquent 集合

      时间:2023-10-31
        <tbody id='Qi58J'></tbody>

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

            • <bdo id='Qi58J'></bdo><ul id='Qi58J'></ul>
              • <small id='Qi58J'></small><noframes id='Qi58J'>

              • 本文介绍了按 created_at 排序 Eloquent 集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个名为posts"的表,其中包含以下列:post_id int primary increments"、poster_id int"和status text",还有一个名为friends"的数组,其列:user_id int primary"和friend_ids"文本'.

                I have a table named 'posts' with the columns: 'post_id int primary increments', 'poster_id int' and 'status text' as well as an array named friends with the columns: 'user_id int primary' and 'friend_ids text'.

                我需要获取朋友文本列中的所有 ID,这很容易使用:

                I need to grab all the IDs in the friends text column which is easy enough using:

                $friends = explode(',', Friend::where('user_id', Sentry::getUser()->id)->first()->friend_ids);
                

                文本列中的数据看起来像1、2、3"等.

                Where the data in the text column would look like '1,2,3,' etc.

                然后我创建一个 Eloquent Collection 对象,这也很容易通过以下方式完成:

                Then I create an Eloquent Collection object which is also easily done via:

                $posts = new IlluminateDatabaseEloquentCollection();
                

                但问题是我不知道如何填充集合并按 Post 对象的created_at"列对其内容进行排序.

                But the problem is I can't figure out how to populate the collection and sort its contents by the Post object's 'created_at' column.

                这是我目前所拥有的:

                foreach ($friends as $id) {
                    $posts_ = Post::where('poster_id', $id)->getQuery()
                        ->orderBy('created_at', 'desc')
                        ->get();
                    foreach($posts_ as $post) {
                        $posts->add($post);
                    }
                }
                

                我不知道这段代码是否适用于按created_at"列对整个帖子集合进行排序.我还需要能够轻松地对整个集合进行分页.

                I can't figure out if this code would work or not for sorting the entire collection of posts by the 'created_at' column. I would also need to be able to paginate the entire collection easily.

                推荐的收藏排序方式是什么?

                What is the recommended way of sorting the collection?

                推荐答案

                如果要对 collection 进行排序,可以使用 sortBy 方法按给定键

                If you want to sort a collection you can use the sortBy method by given key

                $sorted = $posts->sortBy('created_at');
                

                您还可以在 collection

                $sorted = $posts->sortBy(function($post)
                {
                  return $post->created_at;
                });
                

                希望这会有所帮助.有关 collections 的更多信息,您可以阅读 docs

                Hope this helps. For more information on collections you can read the docs

                这篇关于按 created_at 排序 Eloquent 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:required_if laravel 具有多个值 下一篇:Laravel Eloquent 查询生成器默认 Where 条件

                相关文章

              • <small id='1I7MK'></small><noframes id='1I7MK'>

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