doucai4274 2018-11-07 12: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 13: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部