I have a forums system with many categories which has many threads which has many posts.
So with given forum, I should be able to figure out the last post that specific forum has. A forum can have many subforums (aka children). I have only been able to do part of this.
Table demo:
| id | parent_id | name | is_category |
|----|-----------|-------------------|-------------|
| 1 | 0 | Suggestions | 1 |
| 2 | 1 | site suggestions | 0 |
| 3 | 1 | forum suggestions | 0 |
| 4 | 2 | bugs | 0 |
Here's my code:
public function lastPost()
{
foreach ($this->threads()->orderBy('updated_at')->get() as $thread) {
$post = $thread->lastPost();
}
if ($this->hasSubforum()) {
foreach ($this->subforums as $subforum) {
$post = $subforum->lastPost();
}
}
return $post;
}
As you can see, lastPost()
is called until the forum has no more subforums. I am getting the last post from the latest thread in a subforum. So far so good. However, the last post would be the order the subforum is in. So if the last post was in the second last subforum, it returns the last post of the last subforum, since that last forum was returned last in the recursion.
how can I fix this?
Thanks!