On your forumPostsViewedSum you need to also select the forum_thread_id column, so Laravel can reference it back when matching the results:
public function forumPostsViewedSum()
{
return $this->hasMany(ForumPost::class)
->select('forum_thread_id') // <<<<< ADDED
->selectRaw('SUM(viewed) as forum_posts_viewed_sum')
->groupBy('forum_thread_id');
}
But if you want similar functionality as ->withCount(...) you can try doing this instead:
$forumThreads = ForumThread::query()
->with('latestForumPost.creator')
->withCount('forumPosts')
->with('creator')
->getByForumId($forum_id)
->orderBy($order_by, $order_direction)
->offset($limit_start)
->take($forum_threads_per_page)
// ->with('forumPostsViewedSum') // <<<<< REMOVED
->addSelect([
'forum_posts_viewed_sum' => ForumPost::query()
->selectRaw('SUM(viewed)')
->whereColumn('forum_thread_id', 'forum_threads.id'),
])
->withCasts(['forum_posts_viewed_sum' => 'integer'])
->get();
Hope it helps.