File manager - Edit - /home/autoph/public_html/projects/aha-api/app/Models/SurveyEntry.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 SurveyEntry 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 = []; protected $appends = [ 'customer_name', 'entry_date' ]; /** * Boot the entry. * * @return void */ protected static function boot() { parent::boot(); //Prevent submission of entries that don't meet the parent survey's constraints. static::creating(function (self $entry) { $entry->validateCustomer(); $entry->validateMaxEntryPerCustomerRequirement(); }); } /** * The answers within the entry. * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function answers() { return $this->hasMany(SurveyAnswer::class, 'entry_id'); } /** * The survey the entry belongs to. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function survey() { return $this->belongsTo(Survey::class); } /** * The customer that the entry belongs to. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function customer() { return $this->belongsTo(Customer::class, 'customer_id'); } /** * Set the survey the entry belongs to. * * @param Survey $survey * @return $this */ public function for(Survey $survey) { $this->survey()->associate($survey); return $this; } /** * Set the customer who the entry belongs to. * * @param Model|null $model * @return $this */ public function by(Model $model = null) { $this->customer()->associate($model); return $this; } /** * Create an entry from an array. * * @param array $values * @return $this */ public function fromArray(array $values) { foreach ($values as $key => $value) { if ($value === null) { continue; } $answer_class = SurveyAnswer::class; $this->answers->add($answer_class::make([ 'question_id' => substr($key, 1), 'entry_id' => $this->id, 'value' => $value, ])); } return $this; } /** * The answer for a given question. * * @param SurveyQuestion $question * @return mixed|null */ public function answerFor(SurveyQuestion $question) { $answer = $this->answers()->where('question_id', $question->id)->first(); return isset($answer) ? $answer->value : null; } /** * Save the model and all of its relationships. * Ensure the answers are automatically linked to the entry. * * @return bool */ public function push() { $this->save(); foreach ($this->answers as $answer) { $answer->entry_id = $this->id; } return parent::push(); } public function getCustomerNameAttribute() { $customer = Customer::withTrashed()->find($this->customer_id); return ucwords(strtolower("{$customer->firstname} {$customer->lastname}")); } public function getEntryDateAttribute() { return date_format($this->created_at, 'F d, Y h:i A'); } /** * Validate customer's legibility. */ public function validateCustomer() { if ($this->survey->acceptsGuestEntries()) { return; } if ($this->customer_id !== null) { return; } return response()->json(['message' => 'Login is required for this survey.'], 422); } /** * Validate if entry exceeds the survey's * max entry per customer limit. */ public function validateMaxEntryPerCustomerRequirement() { $limit = $this->survey->limitPerCustomer(); if ($limit === null) { return; } $count = static::where('customer_id', $this->customer_id) ->where('survey_id', $this->survey->id) ->count(); if ($count >= $limit) { return response()->json(['message' => 'Maximum entries per customer exceeded.'], 422); } } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings