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

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

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

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

      <legend id='R1hsq'><style id='R1hsq'><dir id='R1hsq'><q id='R1hsq'></q></dir></style></legend>
      <tfoot id='R1hsq'></tfoot>

        Laravel 4 - 雄辯.無限的孩子??變成可用的數(shù)組

        Laravel 4 - Eloquent. Infinite children into usable array?(Laravel 4 - 雄辯.無限的孩子??變成可用的數(shù)組?)
        1. <legend id='4b9UA'><style id='4b9UA'><dir id='4b9UA'><q id='4b9UA'></q></dir></style></legend>
            <i id='4b9UA'><tr id='4b9UA'><dt id='4b9UA'><q id='4b9UA'><span id='4b9UA'><b id='4b9UA'><form id='4b9UA'><ins id='4b9UA'></ins><ul id='4b9UA'></ul><sub id='4b9UA'></sub></form><legend id='4b9UA'></legend><bdo id='4b9UA'><pre id='4b9UA'><center id='4b9UA'></center></pre></bdo></b><th id='4b9UA'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4b9UA'><tfoot id='4b9UA'></tfoot><dl id='4b9UA'><fieldset id='4b9UA'></fieldset></dl></div>

            <small id='4b9UA'></small><noframes id='4b9UA'>

            • <bdo id='4b9UA'></bdo><ul id='4b9UA'></ul>

                  <tbody id='4b9UA'></tbody>
                <tfoot id='4b9UA'></tfoot>

                • 本文介紹了Laravel 4 - 雄辯.無限的孩子??變成可用的數(shù)組?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有一個類別表.每個類別可以有一個可選的父級(如果沒有父級,則默認(rèn)為 0).

                  I've got a categories table. Each category can have an optional parent (Defaults to 0 if no parent).

                  我想要做的是用類別的級別構(gòu)建一個簡單的 html 列表樹.

                  What I want to do is build a simple html list tree with the levels of the categories.

                  示例日期:

                  Foods
                  -- Fruit
                  ---- Apple
                  ---- Banana
                  ---- Orange
                  -- Veg
                  ---- Cucumber
                  ---- Lettuce
                  Drinks
                  -- Alcoholic
                  ---- Beer
                  ---- Vodka
                  Misc
                  -- Household Objects
                  ---- Kitchen
                  ------ Electrical
                  -------- Cooking
                  ---------- Stove
                  ---------- Toaster
                  ---------- Microwave
                  

                  請注意,這需要在大約 10 個級別"內(nèi)工作.我希望它是無限的,但我真的不想走使用嵌套集模型的路線,因?yàn)樗鼤?dǎo)致這個項(xiàng)目的巨大延遲.

                  Note that this needs to work for around 10 'levels'. I'd love it to be infinite but I really dont want to be going down the route of using a nested set model as it'll cause huge delays on this project.

                  關(guān)于 laravel 的文檔很糟糕,甚至沒有真正提到從哪里開始.我已經(jīng)玩了好幾天了,試圖弄清楚該做什么,但如果沒有一個巨大的 for each 循環(huán)塊 10 次,我似乎一無所獲.

                  The docs on this for laravel are terrible, with no real reference as to where to even start. I've been playing with it for days trying to work out what to do and seem to be getting nowhere without a huge messy block of for each loops within each other 10 times.

                  我在我的模型中使用了以下數(shù)據(jù)樹:

                  I've got my tree of data using the following in my model:

                  <?php
                  class Item extends Eloquent {
                      public function parent()
                      {
                          return $this->hasOne('Item', 'id', 'parent_id');
                      }
                  
                      public function children()
                      {
                          return $this->hasMany('Item', 'parent_id', 'id');
                      }
                  
                      public function tree()
                      {
                          return static::with(implode('.', array_fill(0,10, 'children')))->where('parent_id', '=', '0')->get();
                      }
                  }
                  

                  這會將所有父級和子級提升到 10 級.這很好用,但如果不手動在彼此內(nèi)部使用 10 個 foreach 循環(huán),您就無法真正對子數(shù)據(jù)執(zhí)行任何操作.

                  This gets all the parent and children up to a level of 10. This works fine, but you cant really then do anything with the child data without manually having 10 foreach loops within each other.

                  我在這里做錯了什么?當(dāng)然,這不應(yīng)該如此艱難/執(zhí)行不力嗎?我想要做的就是獲得一個簡單的 html 列表,其中包含樹結(jié)構(gòu)中的項(xiàng)目.

                  What am I doing wrong here? Surely this shouldn't be this hard/poorly executed? All I want do do is get a simple html list with the items in a tree structure.

                  我已經(jīng)整理了一個上面使用的虛擬數(shù)據(jù)的快速 SQLFiddle 示例:http://sqlfiddle.com/#!2/e6d18/1

                  I've put together a quick SQLFiddle example of the dummy data used above: http://sqlfiddle.com/#!2/e6d18/1

                  推薦答案

                  這比我平時早上的填字游戲有趣多了.:)

                  This was much more fun than my usual morning crossword puzzle. :)

                  這是一個 ItemsHelper 類,它將執(zhí)行您正在尋找的操作,并且更好地遞歸到您想要的范圍.

                  Here is an ItemsHelper class that will do what you are looking for, and better yet will recurse as far down as you want.

                  app/models/ItemsHelper.php:

                  <?php
                  
                    class ItemsHelper {
                  
                      private $items;
                  
                      public function __construct($items) {
                        $this->items = $items;
                      }
                  
                      public function htmlList() {
                        return $this->htmlFromArray($this->itemArray());
                      }
                  
                      private function itemArray() {
                        $result = array();
                        foreach($this->items as $item) {
                          if ($item->parent_id == 0) {
                            $result[$item->name] = $this->itemWithChildren($item);
                          }
                        }
                        return $result;
                      }
                  
                      private function childrenOf($item) {
                        $result = array();
                        foreach($this->items as $i) {
                          if ($i->parent_id == $item->id) {
                            $result[] = $i;
                          }
                        }
                        return $result;
                      }
                  
                      private function itemWithChildren($item) {
                        $result = array();
                        $children = $this->childrenOf($item);
                        foreach ($children as $child) {
                          $result[$child->name] = $this->itemWithChildren($child);
                        }
                        return $result;
                      }
                  
                      private function htmlFromArray($array) {
                        $html = '';
                        foreach($array as $k=>$v) {
                          $html .= "<ul>";
                          $html .= "<li>".$k."</li>";
                          if(count($v) > 0) {
                            $html .= $this->htmlFromArray($v);
                          }
                          $html .= "</ul>";
                        }
                        return $html;
                      }
                    }
                  

                  我剛剛使用了新安裝的 Laravel 4 和基本的 hello.php 視圖.

                  I just used a new installation of Laravel 4 and the basic hello.php view.

                  這是我在 app/routes.php 中的路線:

                  Here is my route in app/routes.php:

                  Route::get('/', function()
                  {
                    $items = Item::all();
                    $itemsHelper = new ItemsHelper($items);
                    return View::make('hello',compact('items','itemsHelper'));
                  });
                  

                  雖然我的視圖沒有使用 items 變量,但我在這里傳遞它是因?yàn)槟憧赡芤蚕胗盟鼈冏銎渌虑?

                  Although my view doesn't use the items variable, I'm passing it here because you probably will want to do something else with them too.

                  最后,我的 app/views/hello.php 只有一行:

                  And finally, my app/views/hello.php just has one line:

                  htmlList();?>

                  輸出如下:

                  • 食物
                    • 水果
                      • 蘋果
                        • 香蕉
                        • 橙色
                        • 蔬菜
                          • 黃瓜
                          • 生菜
                          • 飲料
                            • 酒精
                              • 啤酒
                              • 伏特加
                              • 雜項(xiàng)
                                • 家居用品
                                  • 廚房
                                    • 電器
                                      • 烹飪
                                        • 爐灶
                                        • 烤面包機(jī)
                                        • 微波爐

                                        注意:您的 SQL Fiddle 有 5(Orange")作為 Cucumber 和 Lettuce 的 parent_id,我不得不將其更改為 6(Veg").

                                        Note: your SQL Fiddle had 5 ("Orange") as the parent_id for Cucumber and Lettuce, I had to change it to 6 ("Veg").

                                        這篇關(guān)于Laravel 4 - 雄辯.無限的孩子??變成可用的數(shù)組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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的訪問被拒絕)

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

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

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

                              <tbody id='xA93S'></tbody>

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

                          • 主站蜘蛛池模板: 久久亚洲国产精品 | 欧美一区二区三区在线 | 91极品欧美视频 | 欧美黑人国产人伦爽爽爽 | 免费三级av| 亚洲成人精品一区 | 精品在线99| 日韩在线资源 | 日韩一区二区三区视频在线播放 | 亚洲444kkkk在线观看最新 | 久久精品欧美电影 | 欧洲一区视频 | 高清18麻豆 | 精品www| 91精品国产欧美一区二区 | 亚洲一一在线 | 美女久久 | 91视视频在线观看入口直接观看 | 国产91久久久久蜜臀青青天草二 | 日韩在线视频一区 | 日韩精品久久一区二区三区 | 亚洲一区视频在线 | 午夜视频网站 | 久久伊 | 久久天堂 | 久久久一区二区三区 | 99re国产视频 | 日日碰狠狠躁久久躁96avv | 久久久久久久久久久久久9999 | 羞羞网站在线免费观看 | 婷婷丁香综合网 | 嫩草懂你的影院入口 | 在线观看国产网站 | 免费午夜电影 | 999久久久免费精品国产 | 国产精品女人久久久 | 亚洲欧美日本国产 | 国产日韩视频在线 | 亚洲一区二区三区在线视频 | 欧美一区二区久久 | 综合久久亚洲 |