* @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; /** * 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 */ 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', ]; } /** * Belongs To A User. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class)->withDefault([ 'username' => 'System', 'id' => User::SYSTEM_USER_ID, ]); } /** * A Poll Has Many Options. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function options(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Option::class); } /** * A Poll Has Many Voters. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(User::class, 'voters')->withTimestamps(); } /** * A Poll Has Many Votes. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function votes(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(Voter::class); } /** * Set The Poll's Title. */ public function setTitleAttribute(string $title): void { $this->attributes['title'] = $title; } }