coolify/app/Models/BaseModel.php

37 lines
814 B
PHP
Raw Normal View History

<?php
namespace App\Models;
2024-12-02 21:49:41 +00:00
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Visus\Cuid2\Cuid2;
abstract class BaseModel extends Model
{
protected static function boot()
{
parent::boot();
static::creating(function (Model $model) {
2023-05-09 12:42:10 +00:00
// Generate a UUID if one isn't set
2024-06-10 20:43:34 +00:00
if (! $model->uuid) {
2024-07-25 11:31:59 +00:00
$model->uuid = (string) new Cuid2;
2023-05-09 12:42:10 +00:00
}
});
}
2024-12-02 21:49:55 +00:00
public function sanitizedName(): Attribute
2024-12-02 21:49:41 +00:00
{
return new Attribute(
2024-12-02 21:49:55 +00:00
get: fn () => sanitize_string($this->getRawOriginal('name')),
2024-12-02 21:49:41 +00:00
);
}
2024-12-02 21:49:55 +00:00
2024-12-02 21:49:41 +00:00
public function image(): Attribute
{
return new Attribute(
2024-12-02 21:49:55 +00:00
get: fn () => sanitize_string($this->getRawOriginal('image')),
2024-12-02 21:49:41 +00:00
);
}
}