pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <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="1rzhv7n" 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 - 雄辯.無限的孩子??變成可用的數組

        Laravel 4 - Eloquent. Infinite children into usable array?(Laravel 4 - 雄辯.無限的孩子??變成可用的數組?)
        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="n7v7hdf" 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 - 雄辯.無限的孩子??變成可用的數組?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

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

                  我想要做的是用類別的級別構建一個簡單的 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 個級別"內工作.我希望它是無限的,但我真的不想走使用嵌套集模型的路線,因為它會導致這個項目的巨大延遲.

                  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.

                  關于 laravel 的文檔很糟糕,甚至沒有真正提到從哪里開始.我已經玩了好幾天了,試圖弄清楚該做什么,但如果沒有一個巨大的 for each 循環塊 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.

                  我在我的模型中使用了以下數據樹:

                  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 級.這很好用,但如果不手動在彼此內部使用 10 個 foreach 循環,您就無法真正對子數據執行任何操作.

                  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.

                  我在這里做錯了什么?當然,這不應該如此艱難/執行不力嗎?我想要做的就是獲得一個簡單的 html 列表,其中包含樹結構中的項目.

                  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.

                  我已經整理了一個上面使用的虛擬數據的快速 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 類,它將執行您正在尋找的操作,并且更好地遞歸到您想要的范圍.

                  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 變量,但我在這里傳遞它是因為你可能也想用它們做其他事情.

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

                  輸出如下:

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

                                        注意:您的 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").

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

                    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="5b7rjlv" id='xA93S'><tfoot id='xA93S'></tfoot><dl id='xA93S'><fieldset id='xA93S'></fieldset></dl></div>

                          • 主站蜘蛛池模板: 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 散热器-电子散热器-型材散热器-电源散热片-镇江新区宏图电子散热片厂家 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 合肥升降机-合肥升降货梯-安徽升降平台「厂家直销」-安徽鼎升自动化科技有限公司 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 软瓷_柔性面砖_软瓷砖_柔性石材_MCM软瓷厂家_湖北博悦佳软瓷 | 浙江美尔凯特智能厨卫股份有限公司 | 洗地机-全自动/手推式洗地机-扫地车厂家_扬子清洁设备 | 石英粉,滑石粉厂家,山东滑石粉-莱州市向阳滑石粉有限公司 | 10吨无线拉力计-2吨拉力计价格-上海佳宜电子科技有限公司 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 联系我们-腾龙公司上分客服微信19116098882 | 香蕉筛|直线|等厚|弧形|振动筛|香蕉筛厂家-洛阳隆中重工 | 加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民 | 四川成人高考_四川成考报名网 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 手板_手板模型制作_cnc手板加工厂-东莞天泓 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 选宝石船-陆地水上开采「精选」色选机械设备-青州冠诚重工机械有限公司 | 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 纸箱网 -纸箱机械|设备|包装纸盒|包装印刷行业门户网站 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 福建成考网-福建成人高考网| 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | AR开发公司_AR增强现实_AR工业_AR巡检|上海集英科技 | 彭世修脚_修脚加盟_彭世修脚加盟_彭世足疗加盟_足疗加盟连锁_彭世修脚技术培训_彭世足疗 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 | CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 27PR跨境电商导航 | 专注外贸跨境电商| KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 披萨石_披萨盘_电器家电隔热绵加工定制_佛山市南海区西樵南方综合保温材料厂 | 建筑消防设施检测系统检测箱-电梯**检测仪器箱-北京宇成伟业科技有限责任公司 | 游戏版号转让_游戏资质出售_游戏公司转让-【八九买卖网】 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 |