Initial commit
This commit is contained in:
45
app/Models/CustomFieldValue.php
Normal file
45
app/Models/CustomFieldValue.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user