• <legend id='8MvBe'><style id='8MvBe'><dir id='8MvBe'><q id='8MvBe'></q></dir></style></legend>

      <tfoot id='8MvBe'></tfoot>

      <small id='8MvBe'></small><noframes id='8MvBe'>

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

        Laravel 按 hasmany 关系排序

        时间:2023-09-24
      2. <i id='XkotX'><tr id='XkotX'><dt id='XkotX'><q id='XkotX'><span id='XkotX'><b id='XkotX'><form id='XkotX'><ins id='XkotX'></ins><ul id='XkotX'></ul><sub id='XkotX'></sub></form><legend id='XkotX'></legend><bdo id='XkotX'><pre id='XkotX'><center id='XkotX'></center></pre></bdo></b><th id='XkotX'></th></span></q></dt></tr></i><div id='XkotX'><tfoot id='XkotX'></tfoot><dl id='XkotX'><fieldset id='XkotX'></fieldset></dl></div>

        <tfoot id='XkotX'></tfoot>
          • <bdo id='XkotX'></bdo><ul id='XkotX'></ul>
          • <small id='XkotX'></small><noframes id='XkotX'>

              <legend id='XkotX'><style id='XkotX'><dir id='XkotX'><q id='XkotX'></q></dir></style></legend>
                    <tbody id='XkotX'></tbody>
                  本文介绍了Laravel 按 hasmany 关系排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有两个 eloquent 模型 ThreadsComments ,每个线程都有很多评论.

                  I have two eloquent models Threads and Comments , each thread hasMany comments.

                  在列出线程时,我需要按 created_at 降序对线程进行排序.因此,我需要在 Comments 中使用 created at 对线程进行排序.

                  While listing the threads, i need to order the threads by the created_at descending. So , i need to sort the threads using created at in Comments.

                  显然点符号对这种排序没有帮助,我如何正确排序线程?

                  Apparently dot notation isn't helpful in ordering this way, how do i order the Threads correctly ?

                  $Threads= Thread::all()->orderBy("comment.created_at","desc")
                  

                  推荐答案

                  了解 Laravel 的预加载是如何工作的很重要.如果我们急切加载您的示例,Laravel 首先获取所有线程.然后它获取所有评论并将它们添加到线程对象.由于使用了单独的查询,因此无法按注释对线程进行排序.

                  It's important to understand how Laravel's eager loading works. If we eager load your example, Laravel first fetches all threads. Then it fetches all comments and adds them to the threads object. Since separate queries are used, it isn't possible to order threads by comments.

                  您需要改用连接.请注意,我在此示例中猜测您的表/列名称.

                  You need to use a join instead. Note that I'm guessing at your table/column names in this example.

                  $threads = Thread::leftJoin('comment', 'comment.thread_id', '=', 'thread.id')
                      ->with('comments')
                      ->orderBy('comment.created_at', 'desc')
                      ->get();
                  

                  自从您加入后,您可能需要手动指定列以选择您的表格列名.

                  Since you're joining, you might need to manually specify columns to select your tables column names.

                  $threads = Thread::select('thread.*')->leftJoin('comment', 'comment.thread_id', '=', 'thread.id')
                      ->with('comments')
                      ->orderBy('comment.created_at', 'desc')
                      ->get();
                  

                  这篇关于Laravel 按 hasmany 关系排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel eloquent 模型模型属性转换(我应该转换哪些 下一篇:Laravel 获取一组关系项

                  相关文章

                  最新文章

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

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

                      <tfoot id='dub44'></tfoot>