@damian0021 look at https://laravel.com/docs/9.x/eloquent-relationships#has-one-of-many
public function lastPost()
{
return $this->hasOne(Post::class)->latestOfMany();
}
and to get it with user details
$threads = Thread::where('category_id', $categories->id)->with('lastPost.user')->get();
assuming Post model hass defined user relationship