* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 */ namespace App\Models; use App\Traits\Auditable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use AllowDynamicProperties; /** * App\Models\Poll. * * @property int $id * @property int $user_id * @property string $title * @property int $multiple_choice * @property \Illuminate\Support\Carbon|null $expires_at * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at */ #[AllowDynamicProperties] final class Poll extends Model { use Auditable; /** @use HasFactory<\Database\Factories\PollFactory> */ use HasFactory; /** * The attributes that aren't mass assignable. * * @var string[] */ protected $guarded = []; /** * Get the attributes that should be cast. * * @return array{expires_at: 'datetime'} */ protected function casts(): array { return [ 'expires_at' => 'datetime', ]; } /** * Get the user that created the poll. * * @return BelongsTo */ public function user(): BelongsTo { return $this->belongsTo(User::class)->withDefault([ 'username' => 'System', 'id' => User::SYSTEM_USER_ID, ]); } /** * Get all options for the poll. * * @return HasMany */ public function options(): HasMany { return $this->hasMany(Option::class); } /** * Get all the users that voted on the poll. * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class, 'voters')->withTimestamps(); } /** * Geth all the votes for the poll. * * @return HasMany */ public function votes(): HasMany { return $this->hasMany(Voter::class); } /** * Set the poll's title. */ public function setTitleAttribute(string $title): void { $this->attributes['title'] = $title; } }