0

Consider a forum which can have many sub-forums which can have more sub-forums.

My forums table is like this:

id, parent_id, name, is_category

A forum category (is_category) cannot have threads in them. However, it can have The parent_id is the id in the forums table; it's a way I can put a sub-forum in a forum. In short, a forum can have many sub-forums which can have many more sub-forums which can have more sub-forums and so on...

Demo Records:

| id | parent_id | name              | is_category |
|----|-----------|-------------------|-------------|
| 1  | 0         | Suggestions       | 1           |
| 2  | 1         | site suggestions  | 0           |
| 3  | 1         | forum suggestions | 0           |
| 4  | 2         | bugs              | 0           |

My view:

@if ($forum->hasSubforum())
    @foreach ($forum->subforums as $subforum)
        {{ $subforum->name }}

        <h1>subforums</h1>
        @if ($forum->hasSubforum())
        @foreach ($subforum->subforums as $child)
            {{ $subforum->name }}
        @endforeach
        @endif
    @endforeach
@endif

What I have tried:

$threads = 0;
while(!$forum->subforums->isEmpty()) {
    $threads += $forum->threads->count();
    while(!$forum->subforums->isEmpty()) {
        $threads += $forum->threads->count();
    }
}

This doesn't even work... :(

TL;DR As you can see, I display the subforms to a user. Butwhat if I want to display how many posts a subforum has? Please do note that a subforum can have more subforums which can then have many threads. I cannot simply do $subforum->threads->count() as that would only count the threads that each subforum has. I also want to count the number of threads all children of the subforum have.

Taylor
  • 2,981
  • 2
  • 29
  • 70
  • [Laravel recursive query answers](http://stackoverflow.com/questions/22014903/laravel-query-builder-for-recursive-results-e-g-id-parent-id). – ourmandave May 07 '16 at 13:21

1 Answers1

1

You can do it with recursion.

function countThreads($forum) {
    $count = $forum->threads->count();
    if ($forum->hasSubforum()) { {
        foreach ($forum->subforums as $subforum) {
            $count += countThreads($subforum);
        }
    }
    return $count;
}
TheDrot
  • 4,246
  • 1
  • 16
  • 26
  • Thanks for this. The `$count = 0` at the top is always making the count 0 when it does recursion... – Taylor May 07 '16 at 15:46
  • If you run the function with deepest subforum as argument do you get correct count? – TheDrot May 07 '16 at 16:41
  • Sorry, I deleted my other comment. No, it doesn't get the correct count. – Taylor May 07 '16 at 17:15
  • Then the problem is somewhere else, because with deepest subforum the recursion does not execute and it should give you the same count as calling $deepestSubforum->threads->count() directly. – TheDrot May 07 '16 at 17:25
  • Sorry, I checked again and it does get the current count. I checked a forum which was not the deepest. So yes, it does get the current count. – Taylor May 07 '16 at 17:40
  • Sometimes I suprise myself with the wtf code. I've updated the answer. – TheDrot May 07 '16 at 18:01