Files
timebank-cc-public/app/Models/PollQuestion.php
T

51 lines
1.1 KiB
PHP

<?php
namespace App\Models;
use App\Polls\Contracts\QuestionType;
use App\Polls\Localized;
use App\Polls\QuestionTypeRegistry;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PollQuestion extends Model
{
use HasFactory;
protected $fillable = ['poll_id', 'position', 'type', 'label', 'settings', 'required'];
protected $casts = [
'label' => 'array',
'settings' => 'array',
'required' => 'boolean',
];
public function poll()
{
return $this->belongsTo(Poll::class);
}
public function answers()
{
return $this->hasMany(PollAnswer::class);
}
/**
* Label in the given (or current) locale, falling back to the app fallback locale
* and then to the first label that is filled in.
*/
public function labelFor(?string $locale = null): string
{
return Localized::pick($this->label, $locale);
}
/**
* The question type handling this question.
*/
public function questionType(): QuestionType
{
return app(QuestionTypeRegistry::class)->get($this->type);
}
}