ldc-com-app-bo/app/Models/Setting.php
2025-05-20 11:45:42 +02:00

74 lines
2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\SettingTypesEnum;
use App\Lib\Helpers\FileStorageHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id Settings id.
* @property string $key Settings key.
* @property string $value Settings value.
* @property \App\Enums\SettingTypesEnum $type Settings type.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*/
class Setting extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'key',
'value',
'type',
'created_at',
'updated_at',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'type' => SettingTypesEnum::class
];
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::updating(function (self $model) {
if (
$model->key === "SquaredLogo" ||
$model->key === "RectangleLogo" ||
$model->key === "BackgroundImage" ||
$model->key === "HomeButtonImage"
) {
$model->value = FileStorageHelper::storeFile($model, $model->value, true);
}
});
static::deleted(function (self $model) {
if (
$model->key === "SquaredLogo" ||
$model->key === "RectangleLogo" ||
$model->key === "BackgroundImage" ||
$model->key === "HomeButtonImage"
) {
FileStorageHelper::removeFile($model, 'value');
}
});
}
}