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.