'boolean', 'is_active' => 'boolean', ]; protected $dates = [ 'created_at', 'updated_at', 'deleted_at', ]; /** * Set value from different field names */ public function setValueTextAttribute($value) { $this->attributes['value'] = $value; } /** * Set value from different field names */ public function setValueBooleanAttribute($value) { $this->attributes['value'] = $value ? '1' : '0'; } /** * Set value from different field names */ public function setValueFileAttribute($value) { $this->attributes['value'] = $value; } /** * Set value from different field names */ public function setValueDateAttribute($value) { $this->attributes['value'] = $value; } /** * Set value from different field names */ public function setValueDatetimeAttribute($value) { $this->attributes['value'] = $value; } /** * Set value from different field names */ public function setValueArrayAttribute($value) { $this->attributes['value'] = is_array($value) ? json_encode($value) : $value; } /** * Get setting by key */ public static function get(string $key, $default = null) { $setting = static::where('key', $key) ->where('is_active', true) ->first(); if (!$setting) { return $default; } return static::castValue($setting->value, $setting->type); } /** * Set setting value */ public static function set(string $key, $value, string $type = 'string'): void { static::updateOrCreate( ['key' => $key], [ 'value' => $value, 'type' => $type, 'is_active' => true, ] ); } /** * Cast value based on type */ protected static function castValue($value, string $type) { return match($type) { 'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN), 'integer' => (int) $value, 'float' => (float) $value, 'array', 'json' => json_decode($value, true), 'file' => $value, // File path as string 'date' => $value, // Date as string (Y-m-d format) 'datetime' => $value, // DateTime as string (Y-m-d H:i:s format) default => $value, }; } /** * Get all settings by group */ public static function getGroup(string $group): array { return static::where('group', $group) ->where('is_active', true) ->pluck('value', 'key') ->map(function ($value, $key) use ($group) { $setting = static::where('key', $key)->first(); return static::castValue($value, $setting->type); }) ->toArray(); } }