'integer', 'post_id' => 'integer', ]; /** * Relationship: Vote belongs to a User. */ public function user() { return $this->belongsTo(User::class, 'user_id', 'ID'); } /** * Relationship: Vote belongs to a Post. */ public function post() { return $this->belongsTo(Post::class, 'post_id', 'ID'); } // ------------------------------------------------------------------------- // Post vote queries // ------------------------------------------------------------------------- public static function getPostVoteCount(int $postId): int { return static::where('post_id', $postId)->count() ?? 0; } public static function hasUserVoted(int $userId, int $postId): bool { return static::where('user_id', $userId) ->where('post_id', $postId) ->count() > 0; } public static function getUserVoteForPost(int $userId, int $postId) { return static::where('user_id', $userId) ->where('post_id', $postId) ->first(); } public static function getPostVotes(int $postId) { return static::where('post_id', $postId)->get(); } public static function deleteUserVoteForPost(int $userId, int $postId): bool { return static::where('user_id', $userId) ->where('post_id', $postId) ->delete() > 0; } public static function deleteAllVotesForPost(int $postId): bool { return !empty(static::where('post_id', $postId)->delete()); } // ------------------------------------------------------------------------- // User vote history // ------------------------------------------------------------------------- public static function getUserVotes(int $userId) { return static::where('user_id', $userId)->get(); } public static function deleteAllVotesByUser(int $userId): bool { return !empty(static::where('user_id', $userId)->delete()); } }