/** * Get the position within a discussion where a post with a certain number * is. If the post with that number does not exist, the index of the * closest post to it will be returned. * * @param integer $discussionId * @param integer $number * @param \Flarum\Core\Models\User|null $user * @return integer */ public function getIndexForNumber($discussionId, $number, User $user = null) { $query = Post::where('discussion_id', $discussionId)->where('time', '<', function ($query) use($discussionId, $number) { $query->select('time')->from('posts')->where('discussion_id', $discussionId)->whereNotNull('number')->take(1)->orderByRaw('ABS(CAST(number AS SIGNED) - ' . (int) $number . ')'); }); return $this->scopeVisibleForUser($query, $user)->count(); }