Files
project-replica/app/Models/Column.php
2026-03-23 00:51:38 +09:00

76 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Column extends Model
{
use HasFactory;
protected $fillable = [
'table_id',
'column_name',
'data_type',
'udt_name',
'ordinal_position',
'is_nullable',
'column_default',
'character_maximum_length',
'numeric_precision',
'numeric_scale',
'datetime_precision',
'comment',
'is_primary_key',
'is_foreign_key',
'is_indexed',
'target_column_name',
'target_data_type',
'exclude_from_migration',
];
protected $casts = [
'ordinal_position' => 'integer',
'is_nullable' => 'boolean',
'is_primary_key' => 'boolean',
'is_foreign_key' => 'boolean',
'is_indexed' => 'boolean',
'exclude_from_migration' => 'boolean',
'character_maximum_length' => 'integer',
'numeric_precision' => 'integer',
'numeric_scale' => 'integer',
];
public function table(): BelongsTo
{
return $this->belongsTo(Table::class);
}
public function getFullTypeAttribute(): string
{
$type = $this->data_type;
if ($this->character_maximum_length) {
$type .= "({$this->character_maximum_length})";
} elseif ($this->numeric_precision && $this->numeric_scale) {
$type .= "({$this->numeric_precision},{$this->numeric_scale})";
} elseif ($this->numeric_precision) {
$type .= "({$this->numeric_precision})";
}
return $type;
}
public function getEffectiveColumnNameAttribute(): string
{
return $this->target_column_name ?? $this->column_name;
}
public function getEffectiveDataTypeAttribute(): string
{
return $this->target_data_type ?? $this->data_type;
}
}