Use built in casts

This commit is contained in:
Lance Pioch 2024-03-17 14:15:06 -04:00
parent 4cc123aed5
commit 0dacfc31ac
7 changed files with 15 additions and 32 deletions

View File

@ -26,12 +26,11 @@ class AuditLog extends Model
protected $table = 'audit_logs'; protected $table = 'audit_logs';
protected bool $immutableDates = true;
protected $casts = [ protected $casts = [
'is_system' => 'bool', 'is_system' => 'bool',
'device' => 'array', 'device' => 'array',
'metadata' => 'array', 'metadata' => 'array',
'created_at' => 'immutable_datetime',
]; ];
protected $guarded = [ protected $guarded = [

View File

@ -36,15 +36,16 @@ class Backup extends Model
protected $table = 'backups'; protected $table = 'backups';
protected bool $immutableDates = true;
protected $casts = [ protected $casts = [
'id' => 'int', 'id' => 'int',
'is_successful' => 'bool', 'is_successful' => 'bool',
'is_locked' => 'bool', 'is_locked' => 'bool',
'ignored_files' => 'array', 'ignored_files' => 'array',
'bytes' => 'int', 'bytes' => 'int',
'completed_at' => 'datetime', 'completed_at' => 'immutable_datetime',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
'deleted_at' => 'immutable_datetime',
]; ];
protected $attributes = [ protected $attributes = [

View File

@ -25,8 +25,6 @@ class DatabaseHost extends Model
*/ */
public const RESOURCE_NAME = 'database_host'; public const RESOURCE_NAME = 'database_host';
protected bool $immutableDates = true;
/** /**
* The table associated with the model. * The table associated with the model.
*/ */
@ -51,6 +49,8 @@ class DatabaseHost extends Model
'id' => 'integer', 'id' => 'integer',
'max_databases' => 'integer', 'max_databases' => 'integer',
'node_id' => 'integer', 'node_id' => 'integer',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
]; ];
/** /**

View File

@ -38,8 +38,6 @@ class EggVariable extends Model
*/ */
public const RESERVED_ENV_NAMES = 'SERVER_MEMORY,SERVER_IP,SERVER_PORT,ENV,HOME,USER,STARTUP,SERVER_UUID,UUID'; public const RESERVED_ENV_NAMES = 'SERVER_MEMORY,SERVER_IP,SERVER_PORT,ENV,HOME,USER,STARTUP,SERVER_UUID,UUID';
protected bool $immutableDates = true;
/** /**
* The table associated with the model. * The table associated with the model.
*/ */
@ -57,6 +55,8 @@ class EggVariable extends Model
'egg_id' => 'integer', 'egg_id' => 'integer',
'user_viewable' => 'bool', 'user_viewable' => 'bool',
'user_editable' => 'bool', 'user_editable' => 'bool',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
]; ];
public static array $validationRules = [ public static array $validationRules = [

View File

@ -19,11 +19,6 @@ abstract class Model extends IlluminateModel
{ {
use HasFactory; use HasFactory;
/**
* Set to true to return immutable Carbon date instances from the model.
*/
protected bool $immutableDates = false;
/** /**
* Determines if the model should undergo data validation before it is saved * Determines if the model should undergo data validation before it is saved
* to the database. * to the database.
@ -160,18 +155,4 @@ abstract class Model extends IlluminateModel
throw new ValidationException($validator); throw new ValidationException($validator);
} }
} }
/**
* Return a timestamp as DateTime object.
*
* @param mixed $value
*/
protected function asDateTime($value): Carbon|CarbonImmutable
{
if (!$this->immutableDates) {
return parent::asDateTime($value);
}
return parent::asDateTime($value)->toImmutable();
}
} }

View File

@ -20,12 +20,14 @@ class RecoveryToken extends Model
public $timestamps = true; public $timestamps = true;
protected bool $immutableDates = true;
public static array $validationRules = [ public static array $validationRules = [
'token' => 'required|string', 'token' => 'required|string',
]; ];
protected $casts = [
'created_at' => 'immutable_datetime',
];
public function user(): BelongsTo public function user(): BelongsTo
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);

View File

@ -22,8 +22,6 @@ class ServerVariable extends Model
*/ */
public const RESOURCE_NAME = 'server_variable'; public const RESOURCE_NAME = 'server_variable';
protected bool $immutableDates = true;
protected $table = 'server_variables'; protected $table = 'server_variables';
protected $guarded = ['id', 'created_at', 'updated_at']; protected $guarded = ['id', 'created_at', 'updated_at'];
@ -31,6 +29,8 @@ class ServerVariable extends Model
protected $casts = [ protected $casts = [
'server_id' => 'integer', 'server_id' => 'integer',
'variable_id' => 'integer', 'variable_id' => 'integer',
'created_at' => 'immutable_datetime',
'updated_at' => 'immutable_datetime',
]; ];
public static array $validationRules = [ public static array $validationRules = [