refactor: rename and enhance setting retrieval methods for improved clarity and functionality

This commit is contained in:
Ümit Tunç
2026-05-21 08:55:21 +03:00
parent da0ee2477b
commit a132da44f9
+21 -8
View File
@@ -320,15 +320,24 @@ class Setting extends Model
return isset($this->attributes['value']) ? json_decode($this->attributes['value'], true) : [];
}
/**
* Find an active setting record by key.
*/
public static function findActiveByKey(string $key): ?self
{
return static::query()
->where('key', $key)
->where('is_active', true)
->first();
}
/**
* Get setting by key
*/
public static function get(string $key, $default = null)
{
$setting = static::where('key', $key)
->where('is_active', true)
->first();
$setting = static::findActiveByKey($key);
if (!$setting) {
return $default;
}
@@ -380,12 +389,16 @@ class Setting extends Model
*/
public static function getGroup(string $group): array
{
return static::where('group', $group)
return static::query()
->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);
->map(function ($value, $key) {
$setting = static::findActiveByKey($key);
return $setting
? static::castValue($value, $setting->type)
: $value;
})
->toArray();
}