Team-Tea-Time/laravel-forum

Threads show first post instead of last

stoffelio opened this issue · 4 comments

After moving my site to a new server the forum shows a strange behavior. Instead of linking to the newest post in the forum overview and the list of current topics, it always links to the first page and first post. So for some reason $thread->lastPost is returning the oldest instead of the newest ..

There are no other issues with the forum or otherwise. Nothing showing in the error logs. Has this been an issue before? Any idea how I could debug this? Server has the same setup as the one beforee ..

Riari commented

Hi,

This issue hasn't been reported before. I'm not exactly sure what could be causing it as accessing the lastPost attribute should query the DB and always return an accurate result (see https://github.com/Team-Tea-Time/laravel-forum/blob/4.0/src/Models/Thread.php#L139-L142).

Can you check the DB and verify the timestamps are correct? Perhaps try SELECT * FROM forum_posts WHERE thread_id = {id} ORDER BY created_at DESC; and see if you get the posts in the order you'd expect (with row 1 being the latest post).

Thanks for the quick response. Requesting that list from the database manually yields the expected result. Everything I try manually works, which is why I was wondering if this had happened before or if maybe there's some secret magic at work here that I didn't account for.

Guess tomorrow I'll go through it again with a fine comb and see if any libraries on the new server differ from the old one or maybe some composer packages were accidentally changed .. although I have a hard time imagining anything causing this.

Either way, this is probably an environment issue on my part, so we can close this issue.

Riari commented

Sure, no problem! Hope you figure it out. I'll close this for now, but feel free to reply again if you identify any related issues in laravel-forum 👍🏻

Quick follow up in case this comes up again: the issue was with the posts relationship in the Thread model being sorted (created_at asc) by default, so another orderBy was only used as a secondary order.

>>> $t->posts()->orderBy('created_at', 'desc')->toSql()
=> "select * from `forum_posts` where `forum_posts`.`thread_id` = ? and `forum_posts`.`thread_id` is not null and `forum_posts`.`deleted_at` is null **order by `created_at` asc, `created_at` desc**"

As a quick fix I made my own posts() function and removed the orderBy, now everything is back to normal. I noticed the default orderBy on posts() was just added in version 4. As I said everything worked fine before my server move, but this might be a problem for others as well.
Edit: not everything, now threads with unread posts are no longer marked. One battle at a time :)