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

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

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

      如何在 Laravel 的 Eloquent ORM 中按数据透视表数据排

      时间:2023-09-23
          <tbody id='uUlf0'></tbody>
          <bdo id='uUlf0'></bdo><ul id='uUlf0'></ul>
            <tfoot id='uUlf0'></tfoot>

            1. <small id='uUlf0'></small><noframes id='uUlf0'>

                <i id='uUlf0'><tr id='uUlf0'><dt id='uUlf0'><q id='uUlf0'><span id='uUlf0'><b id='uUlf0'><form id='uUlf0'><ins id='uUlf0'></ins><ul id='uUlf0'></ul><sub id='uUlf0'></sub></form><legend id='uUlf0'></legend><bdo id='uUlf0'><pre id='uUlf0'><center id='uUlf0'></center></pre></bdo></b><th id='uUlf0'></th></span></q></dt></tr></i><div id='uUlf0'><tfoot id='uUlf0'></tfoot><dl id='uUlf0'><fieldset id='uUlf0'></fieldset></dl></div>
                <legend id='uUlf0'><style id='uUlf0'><dir id='uUlf0'><q id='uUlf0'></q></dir></style></legend>
              • 本文介绍了如何在 Laravel 的 Eloquent ORM 中按数据透视表数据排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在我的数据库中,我有:

                In my Database, I have:

                • tops 表格
                • posts 表格
                • tops_has_posts 表格.
                • tops Table
                • posts Table
                • tops_has_posts Table.

                当我在 tops 表中检索顶部时,我还会检索与顶部相关的 posts.但是如果我想按特定顺序检索这些帖子怎么办?所以我在我的数据透视表 tops_has_posts 中添加了一个 range 字段,我尝试使用 Eloquent 按结果排序,但它不起作用.

                When I retrieve a top on my tops table I also retrieve the posts in relation with the top. But what if I want to retrieve these posts in a certain order ? So I add a range field in my pivot table tops_has_posts and I my trying to order by the result using Eloquent but it doesn't work.

                我试试这个:

                $top->articles()->whereHas('articles', function($q) {
                    $q->orderBy('range', 'ASC');
                })->get()->toArray();
                

                还有这个:

                $top->articles()->orderBy('range', 'ASC')->get()->toArray();
                

                两者都是绝望的尝试.

                提前致谢.

                推荐答案

                有两种方法 - 一种是指定 table.field,另一种使用 Eloquent 别名 pivot_field if你使用 withPivot('field'):

                There are 2 ways - one with specifying the table.field, other using Eloquent alias pivot_field if you use withPivot('field'):

                // if you use withPivot
                public function articles()
                {
                  return $this->belongsToMany('Article', 'tops_has_posts')->withPivot('range');
                }
                
                // then: (with not whereHas)
                $top = Top::with(['articles' => function ($q) {
                  $q->orderBy('pivot_range', 'asc');
                }])->first(); // or get() or whatever
                

                这会起作用,因为 Eloquent 将 withPivot 中提供的所有字段别名为 pivot_field_name.

                This will work, because Eloquent aliases all fields provided in withPivot as pivot_field_name.

                现在,通用解决方案:

                $top = Top::with(['articles' => function ($q) {
                  $q->orderBy('tops_has_posts.range', 'asc');
                }])->first(); // or get() or whatever
                
                // or:
                $top = Top::first();
                $articles = $top->articles()->orderBy('tops_has_posts.range', 'asc')->get();
                

                这将对相关查询进行排序.

                This will order the related query.

                注意:不要因为这样命名事情而使您的生活变得艰难.posts 不一定是 articles,我会使用一个或另一个名称,除非确实需要这样做.

                Note: Don't make your life hard with naming things this way. posts are not necessarily articles, I would use either one or the other name, unless there is really need for this.

                这篇关于如何在 Laravel 的 Eloquent ORM 中按数据透视表数据排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel 过滤所有列中的值 下一篇:如何正确捕获 PHP 异常 (Laravel 5.1)

                相关文章

                最新文章

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

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

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

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