46 lines
1.0 KiB
PHP
46 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
class CustomFieldValue extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'custom_field_id',
|
|
'customizable_type',
|
|
'customizable_id',
|
|
'value',
|
|
];
|
|
|
|
protected $casts = [
|
|
'value' => 'array',
|
|
];
|
|
|
|
public function customField(): BelongsTo
|
|
{
|
|
return $this->belongsTo(CustomField::class);
|
|
}
|
|
|
|
public function customizable(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function getDisplayValueAttribute(): string
|
|
{
|
|
$value = $this->value;
|
|
|
|
return match($this->customField->field_type) {
|
|
CustomField::TYPE_DATE => \Carbon\Carbon::parse($value)->format('Y-m-d'),
|
|
CustomField::TYPE_SELECT => is_array($value) ? implode(', ', $value) : $value,
|
|
default => (string) $value,
|
|
};
|
|
}
|
|
}
|