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

    • <bdo id='3RKmP'></bdo><ul id='3RKmP'></ul>
  • <tfoot id='3RKmP'></tfoot>

      <small id='3RKmP'></small><noframes id='3RKmP'>

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

        Laravel 獲取祖先(URL)

        Laravel Get ancestors (URL)(Laravel 獲取祖先(URL))

        <tfoot id='sdbHr'></tfoot>

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

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

                <tbody id='sdbHr'></tbody>
              • <bdo id='sdbHr'></bdo><ul id='sdbHr'></ul>

                  本文介紹了Laravel 獲取祖先(URL)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在 Laravel 中,我有一個包含 id、parent_id、slug (Self-referring) 的表,

                  In Laravel, I have a table which contains id, parent_id, slug (Self-referring),

                  當我有一個ID時,我需要以這樣的格式(以/"分隔)獲取其所有祖先.

                  When I have an ID, I need to get all its ancestors in a format like this (Separated by "/").

                  level1/level2/level3
                  

                  但是在沒有像laravel-nestedset"這樣的包的情況下以一種有效的方式".

                  But in an efficient way without a package like "laravel-nestedset ".

                  我是這樣實現的.

                  public function parent()
                  {
                      return $this->belongsTo('Collection', 'parent_id');
                  }
                  
                  public function getParentsAttribute()
                  {
                      $parents = collect([]);
                  
                      $parent = $this->parent;
                  
                      while(!is_null($parent)) {
                          $parents->push($parent);
                          $parent = $parent->parent;
                      }
                  
                      return $parents;
                  }
                  

                  還有其他方法可以有效地做到這一點并用/"分隔嗎?

                  Any other way to do it efficiently and separated by "/" ?

                  推薦答案

                  在評論中進行了一些交談后,我認為這是一個很好的解決方案:

                  After a little conversation in the comments I think this is a good solution:

                  // YourModel.php
                  
                  // Add this line of you want the "parents" property to be populated all the time.
                  protected $appends = ['parents'];
                  
                  public function getParentsAttribute()
                  {
                      $collection = collect([]);
                      $parent = $this->parent;
                      while($parent) {
                          $collection->push($parent);
                          $parent = $parent->parent;
                      }
                  
                      return $collection;
                  }
                  

                  然后您可以使用以下方法檢索您的父母:

                  Then you can retrieve your parents using:

                  • YourModel::find(123)->parents(集合實例)
                  • YourModel::find(123)->parents->implode('yourprop', '/')(內爆為字符串,見 https://laravel.com/docs/5.4/collections#method-implode)
                  • YourModel::find(123)->parents->reverse()->implode('yourprop', '/')(倒序https://laravel.com/docs/5.4/collections#method-reverse)
                  • YourModel::find(123)->parents (collection instance)
                  • YourModel::find(123)->parents->implode('yourprop', '/') (imploded to string, see https://laravel.com/docs/5.4/collections#method-implode)
                  • YourModel::find(123)->parents->reverse()->implode('yourprop', '/') (reversed order https://laravel.com/docs/5.4/collections#method-reverse)

                  正如 Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 所指出的,您也可以將它組合起來這樣可以節省一些查詢:

                  As noted by Nikolai Kiselev https://stackoverflow.com/a/55103589/1346367 you may also combine it with this to save a few queries:

                  protected $with = ['parent.parent.parent'];
                  // or inline:
                  YourModel::find(123)->with(['parent.parent.parent']);
                  

                  這會在對象加載時預加載父級.如果您決定不使用它,則在您調用 $yourModel->parent 時(延遲)加載父項.

                  This preloads the parent on object load. If you decide not to use this, the parent is (lazy) loaded as soon as you call $yourModel->parent.

                  這篇關于Laravel 獲取祖先(URL)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

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

                    <bdo id='E2BeA'></bdo><ul id='E2BeA'></ul>
                          1. <tfoot id='E2BeA'></tfoot>
                              <tbody id='E2BeA'></tbody>

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

                            <i id='E2BeA'><tr id='E2BeA'><dt id='E2BeA'><q id='E2BeA'><span id='E2BeA'><b id='E2BeA'><form id='E2BeA'><ins id='E2BeA'></ins><ul id='E2BeA'></ul><sub id='E2BeA'></sub></form><legend id='E2BeA'></legend><bdo id='E2BeA'><pre id='E2BeA'><center id='E2BeA'></center></pre></bdo></b><th id='E2BeA'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='E2BeA'><tfoot id='E2BeA'></tfoot><dl id='E2BeA'><fieldset id='E2BeA'></fieldset></dl></div>
                            主站蜘蛛池模板: 99国产精品久久久 | 亚洲精品中文字幕av | 亚洲一区中文字幕 | 日日夜夜狠狠操 | 亚洲精品一区二区在线 | 国产精品欧美一区二区三区不卡 | 精品日韩一区二区 | 欧美综合久久 | 日韩精品在线观看网站 | 欧美日韩高清免费 | 国产在线一区二区 | 久久高清国产 | 中文字幕 亚洲一区 | 少妇特黄a一区二区三区88av | 久久综合欧美 | 一区二区在线不卡 | www网站在线观看 | 毛片毛片毛片毛片毛片 | 91在线精品一区二区 | 久久久亚洲一区 | 国产偷录视频叫床高潮对白 | 人人干人人艹 | 我爱操| 91一区二区三区 | 五月综合激情网 | 亚洲一区二区三区免费在线 | 超碰综合 | 在线观看免费福利 | 午夜精品一区 | 欧产日产国产精品99 | 成人精品 | 欧美综合视频 | 欧美激情久久久久久 | 在线黄 | 成年人视频免费在线观看 | 1000部精品久久久久久久久 | 中文字字幕一区二区三区四区五区 | 欧美精品综合在线 | 亚洲精品乱码久久久久v最新版 | 国产精品一级 | 丁香综合 |