1. <tfoot id='nmCo3'></tfoot>

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

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

        使用 Laravel/Eloquent 订购相关模型

        Ordering Related Models with Laravel/Eloquent(使用 Laravel/Eloquent 订购相关模型)
            <tbody id='bAEpi'></tbody>

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

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

              <tfoot id='bAEpi'></tfoot>

            1. <i id='bAEpi'><tr id='bAEpi'><dt id='bAEpi'><q id='bAEpi'><span id='bAEpi'><b id='bAEpi'><form id='bAEpi'><ins id='bAEpi'></ins><ul id='bAEpi'></ul><sub id='bAEpi'></sub></form><legend id='bAEpi'></legend><bdo id='bAEpi'><pre id='bAEpi'><center id='bAEpi'></center></pre></bdo></b><th id='bAEpi'></th></span></q></dt></tr></i><div id='bAEpi'><tfoot id='bAEpi'></tfoot><dl id='bAEpi'><fieldset id='bAEpi'></fieldset></dl></div>
                  <legend id='bAEpi'><style id='bAEpi'><dir id='bAEpi'><q id='bAEpi'></q></dir></style></legend>
                  本文介绍了使用 Laravel/Eloquent 订购相关模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以将 orderBy 用于对象的相关模型?也就是说,假设我有一个带有 hasMany("Comments"); 的博客帖子模型,我可以使用

                  Is it possible to use an orderBy for an object's related models? That is, let's say I have a Blog Post model with a hasMany("Comments"); I can fetch a collection with

                  $posts = BlogPost::all();
                  

                  然后遍历每个帖子,并显示每个帖子的评论上次编辑日期

                  And then run through each post, and display the comment's last edited date for each one

                  foreach($posts as $post)
                  {
                      foreach($post->comments as $comment)
                      {
                          echo $comment->edited_date,"
                  ";
                      }
                  }
                  

                  有没有办法让我设置评论的返回顺序?

                  Is there a way for me to set the order the comments are returned in?

                  推荐答案

                  关系返回的对象是一个 Eloquent 实例,支持查询构建器的功能,因此可以在其上调用查询构建器的方法.

                  The returned object from the relationship is an Eloquent instance that supports the functions of the query builder, so you can call query builder methods on it.

                  foreach ($posts as $post) {
                      foreach ($post->comments()->orderBy('edited_date')->get() as $comment) {
                          echo $comment->edited_date,"
                  ";
                      }
                  }
                  

                  另外,当你 foreach() 像这样的所有帖子时,请记住,Laravel 必须运行查询以在每次迭代中选择帖子的评论,所以 热切加载 就像您在 推荐使用 Jarek Tkaczyk 的答案.

                  Also, keep in mind when you foreach() all posts like this, that Laravel has to run a query to select the comments for the posts in each iteration, so eager loading the comments like you see in Jarek Tkaczyk's answer is recommended.

                  您也可以像在这个问题中看到的那样,为有序评论创建一个独立的函数.一>.

                  You can also create an independent function for the ordered comments like you see in this question.

                  public function comments() {
                      return $this->hasMany('Comment')->orderBy('comments.edited_date');
                  }
                  

                  然后您可以像在原始代码中那样循环它们.

                  And then you can loop them like you did in your original code.

                  这篇关于使用 Laravel/Eloquent 订购相关模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <tbody id='nKqTJ'></tbody>

                    <tfoot id='nKqTJ'></tfoot>

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

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

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