*/ #[\Override] protected $casts = [ 'submitted_at' => 'datetime', 'played_at' => 'datetime', 'is_played' => 'boolean', 'is_approved' => 'boolean', 'votes' => 'integer', ]; public function votes(): HasMany { return $this->hasMany(RadioSongVote::class); } public function isPending(): bool { return ! $this->is_played && ! $this->played_at; } public function isQueued(): bool { return $this->is_approved && ! $this->is_played; } public function isPlayed(): bool { return $this->is_played || $this->played_at !== null; } public function approve(): void { $this->update(['is_approved' => true]); } public function markAsPlayed(): void { $this->update([ 'is_played' => true, 'played_at' => now(), ]); } public function voteCount(): int { return $this->votes()->count(); } public function hasUserVoted(User $user): bool { return $this->votes()->where('user_id', $user->id)->exists(); } public function addVote(User $user): bool { if ($this->hasUserVoted($user)) { return false; } RadioSongVote::create([ 'song_request_id' => $this->id, 'user_id' => $user->id, ]); $this->increment('votes'); return true; } public function getQueuePosition(): int { return self::where('is_approved', true) ->where('is_played', false) ->orderBy('votes', 'desc') ->orderBy('submitted_at', 'asc') ->pluck('id') ->search($this->id) + 1; } public function scopeQueued($query) { return $query->where('is_approved', true) ->where('is_played', false) ->orderBy('votes', 'desc') ->orderBy('submitted_at', 'asc'); } public function scopePending($query) { return $query->where('is_approved', false) ->orderBy('submitted_at', 'desc'); } }