refactor: rename and enhance setting retrieval methods for improved clarity and functionality
This commit is contained in:
+21
-8
@@ -320,15 +320,24 @@ class Setting extends Model
|
|||||||
return isset($this->attributes['value']) ? json_decode($this->attributes['value'], true) : [];
|
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
|
* Get setting by key
|
||||||
*/
|
*/
|
||||||
public static function get(string $key, $default = null)
|
public static function get(string $key, $default = null)
|
||||||
{
|
{
|
||||||
$setting = static::where('key', $key)
|
$setting = static::findActiveByKey($key);
|
||||||
->where('is_active', true)
|
|
||||||
->first();
|
|
||||||
|
|
||||||
if (!$setting) {
|
if (!$setting) {
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
@@ -380,12 +389,16 @@ class Setting extends Model
|
|||||||
*/
|
*/
|
||||||
public static function getGroup(string $group): array
|
public static function getGroup(string $group): array
|
||||||
{
|
{
|
||||||
return static::where('group', $group)
|
return static::query()
|
||||||
|
->where('group', $group)
|
||||||
->where('is_active', true)
|
->where('is_active', true)
|
||||||
->pluck('value', 'key')
|
->pluck('value', 'key')
|
||||||
->map(function ($value, $key) use ($group) {
|
->map(function ($value, $key) {
|
||||||
$setting = static::where('key', $key)->first();
|
$setting = static::findActiveByKey($key);
|
||||||
return static::castValue($value, $setting->type);
|
|
||||||
|
return $setting
|
||||||
|
? static::castValue($value, $setting->type)
|
||||||
|
: $value;
|
||||||
})
|
})
|
||||||
->toArray();
|
->toArray();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user