Files
UNIT3D/app/Models/Poll.php

111 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @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<User, $this>
*/
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<Option, $this>
*/
public function options(): \Illuminate\Database\Eloquent\Relations\HasMany
{
return $this->hasMany(Option::class);
}
/**
* A Poll Has Many Voters.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<User, $this>
*/
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<Voter, $this>
*/
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;
}
}