mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-21 13:23:04 +01:00
Order/location of columns is now variable
This commit is contained in:
51
app/Casts/CommaSeparatedArrayCast.php
Normal file
51
app/Casts/CommaSeparatedArrayCast.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Casts;
|
||||
|
||||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||||
|
||||
class CommaSeparatedArrayCast implements CastsAttributes
|
||||
{
|
||||
/**
|
||||
* Array of settings that should be cast
|
||||
*/
|
||||
private array $shouldCast = [
|
||||
'visible_columns',
|
||||
];
|
||||
|
||||
/**
|
||||
* Cast the given value.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($model, $key, $value, $attributes)
|
||||
{
|
||||
if (!in_array($model->name, $this->shouldCast)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return explode(',', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the given value for storage.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Model $model
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param array $attributes
|
||||
* @return mixed
|
||||
*/
|
||||
public function set($model, $key, $value, $attributes)
|
||||
{
|
||||
if (!in_array($model->name, $this->shouldCast)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return implode(',', $value);
|
||||
}
|
||||
}
|
||||
@@ -168,6 +168,9 @@ class SettingsHelper
|
||||
'telegram_bot_token' => SettingsHelper::settingIsEditable('telegram_bot_token'),
|
||||
'telegram_chat_id' => SettingsHelper::settingIsEditable('telegram_chat_id'),
|
||||
],
|
||||
'tables' => [
|
||||
'visible_columns' => SettingsHelper::get('visible_columns')->value,
|
||||
],
|
||||
'auth' => (bool)SettingsHelper::get('auth')->value
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Casts\CommaSeparatedArrayCast;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
@@ -17,4 +18,8 @@ class Setting extends Model
|
||||
];
|
||||
|
||||
protected $table = 'settings';
|
||||
|
||||
protected $casts = [
|
||||
'value' => CommaSeparatedArrayCast::class,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user