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

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

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

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

      <bdo id='hVIeL'></bdo><ul id='hVIeL'></ul>
  2. <tfoot id='hVIeL'></tfoot>

    1. Laravel 在同一個表中返回父母的孩子

      Laravel returning children of parents in same table(Laravel 在同一個表中返回父母的孩子)
    2. <legend id='DSWoN'><style id='DSWoN'><dir id='DSWoN'><q id='DSWoN'></q></dir></style></legend>

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

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

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

              1. 本文介紹了Laravel 在同一個表中返回父母的孩子的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                使用 Laravel 5.1,我嘗試從 MySQL 類別表創建菜單列表.我的服務提供者返回數據,但我不明白如何在 foreach 循環中創建子類別.當我執行循環時,只返回子查詢的最后一行.任何指導將不勝感激.

                Using Laravel 5.1, I am trying to create a menu list from a MySQL categories table. My service provider returns data, but I don't understand how to create the child categories in a foreach loop. When I perform the loop, only the last row of the child query is returned. Any guidance would be appreciated.

                類別表

                id  | cat_name      | cat_parent_id
                --- | --------------| ------------- 
                1   | Parent Cat 1  | NULL 
                2   | Parent Cat 2  | NULL 
                3   | Child Cat 1   | 2 
                4   | Child Cat 2   | 2 
                5   | Parent Cat 3  | NULL 
                6   | Child Cat 3   | 5
                

                預期結果

                Parent Cat 1
                Parent Cat 2
                    Child Cat 1
                    Child Cat 2
                Parent Cat 3
                    Child Cat 3
                

                viewComposerServiceProvider.php

                public function boot()
                {
                       $this->composeTopCategoryNavigation();
                       $this->composeSubCategoryNavigation();
                }
                
                private function composeTopCategoryNavigation()
                {
                    view()->composer('partials.header', function($view)
                        {
                            $view->with('top_cats', Category::whereNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
                        });
                }
                
                private function composeSubCategoryNavigation()
                {
                        view()->composer('partials.header', function($view)
                        {
                            $view->with('sub_cats', Category::whereNotNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
                        });
                }
                

                標題視圖

                <ul>
                @foreach ($top_cats as $top_cat)
                  <?php $top_cat_slug = str_slug( $top_cat->cat_name, "-"); ?>
                  <li>{{ $top_cat->cat_name }}
                    @foreach ($sub_cats as $sub_cat)
                            @if ( $sub_cat->cat_parent_id === $top_cat->id )
                                <ul>
                                  <li{{ $sub_cat->cat_name }}</li>
                                </ul>
                                @endif
                        @endforeach
                   </li>
                @endforeach
                </ul>
                

                推薦答案

                首先,你的做法是低效的.您的視圖遍歷每個父類別的所有子類別.如果您正確定義關系并利用 Eloquent 的預先加載,您可以更輕松地獲取和訪問子類別.

                First of all, what you are doing is inefficient. Your view iterates through all subcategories for every parent category. If you defined relations properly and made use of Eloquent's eager loading, you could fetch and access child categories in easier way.

                從定義關系開始:

                class Category extends Model {
                  //each category might have one parent
                  public function parent() {
                    return $this->belongsToOne(static::class, 'cat_parent_id');
                  }
                
                  //each category might have multiple children
                  public function children() {
                    return $this->hasMany(static::class, 'cat_parent_id')->orderBy('cat_name', 'asc');
                  }
                }
                

                一旦您正確定義了關系,您就可以獲取整個類別樹,如下所示:

                Once you have the relations defined properly, you can fetch whole category tree like below:

                view()->composer('partials.header', function($view) {
                  $view->with('categories', Category::with('children')->whereNull('cat_parent_id')->orderBy('cat_name', 'asc')->get());
                });
                

                不需要第二個作曲家,因為父類別已經包含子類別.

                The second composer won't be needed, as parent categories already contain children.

                現在,您只需要在視圖中顯示類別:

                Now, you only need to display the categories in your view:

                <ul>
                  @foreach ($categories as $parent)
                    <li>{{ $parent->cat_name }}
                      @if ($parent->children->count())
                        <ul>
                          @foreach ($parent->children as $child)
                            <li>{{ $child->cat_name }}</li>
                          @endforeach
                        </ul>
                      @endif
                    </li>
                  @endforeach
                </ul>
                

                這篇關于Laravel 在同一個表中返回父母的孩子的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                相關文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                  <tbody id='1Qdav'></tbody>

              2. <small id='1Qdav'></small><noframes id='1Qdav'>

                  <legend id='1Qdav'><style id='1Qdav'><dir id='1Qdav'><q id='1Qdav'></q></dir></style></legend>
                1. <tfoot id='1Qdav'></tfoot>
                      <bdo id='1Qdav'></bdo><ul id='1Qdav'></ul>

                        • <i id='1Qdav'><tr id='1Qdav'><dt id='1Qdav'><q id='1Qdav'><span id='1Qdav'><b id='1Qdav'><form id='1Qdav'><ins id='1Qdav'></ins><ul id='1Qdav'></ul><sub id='1Qdav'></sub></form><legend id='1Qdav'></legend><bdo id='1Qdav'><pre id='1Qdav'><center id='1Qdav'></center></pre></bdo></b><th id='1Qdav'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='1Qdav'><tfoot id='1Qdav'></tfoot><dl id='1Qdav'><fieldset id='1Qdav'></fieldset></dl></div>
                          主站蜘蛛池模板: 欧美一级淫片007 | 欧美在线视频一区 | 中文字幕在线人 | 91精品国产综合久久福利软件 | 国产亚洲网站 | 亚洲精品国产a久久久久久 午夜影院网站 | 久久久欧洲 | 一区二区视频在线 | 免费视频一区二区 | 日日夜夜天天久久 | 国产精品小视频在线观看 | 久久精品91久久久久久再现 | 国产 日韩 欧美 制服 另类 | 欧美一区二区在线观看 | 美日韩免费视频 | 黄色一级大片在线免费看产 | 欧美精品一区二区三区在线 | 国产欧美一区二区三区在线看 | 精品国模一区二区三区欧美 | 欧美精品在线一区二区三区 | 在线观看视频中文字幕 | 欧美精品一区二区三区四区 在线 | 久久久久香蕉视频 | 精品国产一区二区三区av片 | 精品国产不卡一区二区三区 | 中文字幕一区二区三区不卡 | 精品99久久 | 久久久久久久久久久久亚洲 | 在线播放国产一区二区三区 | 久久99久久99久久 | 亚洲一二三区在线观看 | 一级黄色夫妻生活 | 亚洲日产精品 | 中文字幕精品一区 | 天天综合91| 国产婷婷精品av在线 | 国产剧情一区 | 中文字幕在线第一页 | 999久久久国产精品 欧美成人h版在线观看 | 国产精品一区二区欧美黑人喷潮水 | 91精品国产91久久久久游泳池 |