久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <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 class="qwawimqqmiuu" 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>
      2. 如何在 Laravel 的 Eloquent ORM 中按數(shù)據(jù)透視表數(shù)據(jù)排

        How to order by pivot table data in Laravel#39;s Eloquent ORM(如何在 Laravel 的 Eloquent ORM 中按數(shù)據(jù)透視表數(shù)據(jù)排序)

            <tbody id='chvMb'></tbody>
          <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 class="qwawimqqmiuu" 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 中按數(shù)據(jù)透視表數(shù)據(jù)排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  在我的數(shù)據(jù)庫中,我有:

                  In my Database, I have:

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

                  當(dāng)我在 tops 表中檢索頂部時,我還會檢索與頂部相關(guān)的 posts.但是如果我想按特定順序檢索這些帖子怎么辦?所以我在我的數(shù)據(jù)透視表 tops_has_posts 中添加了一個 range 字段,我嘗試使用 Eloquent 按結(jié)果排序,但它不起作用.

                  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.

                  現(xiàn)在,通用解決方案:

                  $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();
                  

                  這將對相關(guān)查詢進行排序.

                  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.

                  這篇關(guān)于如何在 Laravel 的 Eloquent ORM 中按數(shù)據(jù)透視表數(shù)據(jù)排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)

                    <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 class="qwawimqqmiuu" 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'>

                            <tbody id='qWF2o'></tbody>
                          • <tfoot id='qWF2o'></tfoot>
                            主站蜘蛛池模板: 蜜桃av一区二区三区 | 天天天操| 亚洲欧美一区二区三区情侣bbw | 久久精品免费 | 亚洲激情第一页 | 国产精品成av人在线视午夜片 | 中文字幕av一区二区三区 | 91视频.com | 国产日韩精品一区 | 免费观看毛片 | 97人人超碰 | 成人二区 | 欧美精品一区二区三区在线播放 | 日韩精品免费看 | 二区国产 | 久久久久久久久99 | 欧美日韩亚洲在线 | 亚洲高清视频一区 | 天天操天天射综合网 | 免费黄色日本 | 日韩成人在线播放 | 日韩一区二区三区av | 久久高清精品 | 精品国产一区二区久久 | 精品视频在线观看 | 另类二区 | 久久国产精品无码网站 | 亚洲福利一区 | 一区二区三区四区在线播放 | 日韩国产精品一区二区三区 | 国产二区精品视频 | 国产精品福利视频 | 国产三级国产精品 | 亚洲在线成人 | 日韩精品在线一区 | 久久精品手机视频 | 伊人色综合久久天天五月婷 | 91观看 | 久久精品久久久久久 | 久久国内精品 | 免费看一区二区三区 |