doucai4274 2018-11-07 20:50
浏览 43
已采纳

Laravel嵌套儿童计数(类别)

I using nested categories on my Laravel site with an adjacentcy list style mysql table layout. There is a maximum of 7 layers of categorization, (using google categories taxonomy). Please see the image below for a small sample of data, and the code below for my Category model's relationships.

Each category has zero to many subcategories (using same Category Model), as well as zero to many pages underneath it. My goal here is to find a way to get a count of all pages beneath a category (including pages under its subcategories).

MySql Table Layout Example

My Category model has the following relationships which are all functioning:

public function pages() {
    return $this->hasMany('App\Models\Page');
}

public function children() {
    return $this->hasMany('App\Models\Category', 'parent_id' );
}

public function parent() {
    return $this->belongsTo('App\Models\Category', 'parent_id' );
}

public function childrenRecursive() {
   return $this->children()->orderBy( 'name' )->with('childrenRecursive', 'pages');
}

public function parentRecursive() {
   return $this->parent()->with('parentRecursive');
}
  • 写回答

2条回答 默认 最新

  • duanjiong1952 2018-11-07 21:48
    关注

    Because I am eager loading all of these categories and pages in the first palce using the following line of code and my Categories model:

    $categories = Category::with( 'childrenRecursive' )->whereNull( 'parent_id' )->get();
    

    I was able to use a recursive formula to sum up the number of pages underneath each category without slowing down the page:

    foreach( $categories as $category ) {
        $category->pagesCount = 0;
        $category->pagesCount += $category->pages->count();
        foreach( $category->childrenRecursive as $child ) {
            $category->pagesCount += countChildPages( $child );
        }
    }
    
    function countChildPages( $category ) {
        $category->pagesCount = 0;
        $category->pagesCount += $category->pages->count();
        foreach( $category->childrenRecursive as $child ) {
            $category->pagesCount += countChildPages( $child );
        }
        return $category->pagesCount;
    }
    

    I then just accessed the count ($category->pagesCount) whenever I needed to use it on the page.




    Example of sitemap.html page:

    foreach( $categories as $category ) {
        $category->pagesCount = 0;
        $category->pagesCount += $category->pages->count();
        foreach( $category->childrenRecursive as $child ) {
            $category->pagesCount += countChildPages( $child );
        }
    }
    
    foreach( $categories as $category ) {
        if( $category->pagesCount > 0 ) {
            echo '<div class="category">';
                echo '<h2><a href="https://example.com/' . $category->slug . '">' . $category->name . ' (' . $category->pagesCount . ')</a></h2>';
                foreach( $category->pages as $page ) {
                    showPage( $page );
                }
                foreach( $category->childrenRecursive as $child ) {
                    showSubCategory( $child );
                }
            echo '</div>';
        }
    }
    
    function countChildPages( $category ) {
        $category->pagesCount = 0;
        $category->pagesCount += $category->pages->count();
        foreach( $category->childrenRecursive as $child ) {
            $category->pagesCount += countChildPages( $child );
        }
        return $category->pagesCount;
    }
    
    function showSubCategory( $category ) {
        if( $category->pagesCount > 0 ) {
            echo '<div class="category">';
                echo '<h2><a href="https://example.com/' . $category->slug . '">' . $category->name . ' (' . $category->pagesCount . ')</a></h2>';
                foreach( $category->pages as $page ) {
                    showPage( $page );
                }
                foreach( $category->childrenRecursive as $child ) {
                    showSubCategory( $child );
                }
            echo '</div>';
        }
    }
    
    function showPage( $page ) {
        echo '<div class="category page">';
            echo '<h2><a href="https://example.com/' . $page->slug . '">' . $page->title . '</a></h2>';
        echo '</div>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面