File manager - Edit - /home/autoph/public_html/projects/aha-api/app/Models/SurveyQuestion.php
Back
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use OwenIt\Auditing\Contracts\Auditable; use Illuminate\Database\Eloquent\SoftDeletes; use OwenIt\Auditing\Auditable as AuditableTrait; use Illuminate\Database\Eloquent\Factories\HasFactory; class SurveyQuestion extends Model implements Auditable { use HasFactory, SoftDeletes, AuditableTrait; /** * The attributes that are NOT mass assignable. * */ protected $guarded = ['deleted_at']; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'rules' => 'array', 'options' => 'array', 'isActive' => 'boolean', ]; protected $appends = [ 'survey_name', 'section_name', 'rating_min', 'rating_max', ]; /** * Boot the question. * * @return void */ protected static function boot() { parent::boot(); //Ensure the question's survey is the same as the section it belongs to. static::creating(function (self $question) { $question->load('section'); if ($question->section) { $question->survey_id = $question->section->survey_id; } }); } /** * The survey the question belongs to. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function survey() { return $this->belongsTo(Survey::class); } /** * The section the question belongs to. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function section() { return $this->belongsTo(SurveySection::class); } /** * The answers that belong to the question. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function answers() { return $this->hasMany(SurveyAnswer::class, 'question_id'); } public function getSurveyNameAttribute() { return Survey::withTrashed()->find($this->survey_id)->name; } public function getSectionNameAttribute() { $section = SurveySection::withTrashed()->find($this->section_id); return $section->name ?? null; } /** * The question's validation rules. * * @param $value * @return array|mixed */ public function getRulesAttribute($value) { $value = $this->castAttribute('rules', $value); return $value !== null ? $value : []; } /** * The unique key representing the question. * * @return string */ public function getKeyAttribute() { return "q{$this->id}"; } public function getRatingMinAttribute() { return $this->options[0] ?? null; } public function getRatingMaxAttribute() { return $this->options[1] ?? null; } /** * Scope a query to only include questions that * don't belong to any sections. * * @param $query * @return mixed */ public function scopeWithoutSection($query) { return $query->where('section_id', null); } public function scopeRatings($query) { return $query->where('type', 'rating'); } public function scopeActive($query) { return $query->where('isActive', true); } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings