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条)

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗