Skip to content

Conversation

serious-angel
Copy link
Contributor

@serious-angel serious-angel commented Jul 19, 2025

Dear Developers,

Thank you very much for the marvel you do!


Example

Laravel Data

namespace App\Data\Settings\Background;

use App\Enums\Color;
use Spatie\LaravelData\Attributes\MergeValidationRules;
use Spatie\LaravelData\Data;

#[MergeValidationRules]
final class ColorData extends Data
{
    public function __construct(
        public Color $name,
        public float $brightness,
        public float $contrast,
        public float $saturation,
    ) {}
}

Laravel Settings

namespace App\Settings\Background;

use App\Data\Settings\Background\ColorData;
use Spatie\LaravelSettings\Settings;
use Spatie\LaravelSettings\SettingsCasts\ArrayDataCast;

class ColorSettings extends Settings
{
    public bool $enabled = false;

    /** @var ColorData[] */
    public array $colors = [];

    public static function group(): string
    {
        return 'background:color';
    }

    public static function casts(): array
    {
        return [
            'colors' => ArrayDataCast::class . ':' . ColorData::class,
        ];
    }
}

Migration

use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class() extends SettingsMigration {
    public function up(): void
    {
        $this->migrator->add('background:color.enabled', false);
        $this->migrator->add('background:color.colors', []);
    }

    public function down(): void
    {
        $this->migrator->deleteIfexists('background:color.enabled');
        $this->migrator->deleteIfexists('background:color.colors');
    }
};

Test

$ p-artisan tinker;
Psy Shell v0.11.22 (PHP 8.1.32 — cli) by Justin Hileman
> $s = app(ColorSettings::class);
[!] Aliasing 'ColorSettings' to 'App\Settings\ColorSettings' for this Tinker session.
[!] Aliasing 'ColorData' to 'App\Data\Settings\Background\ColorData' for this Tinker session.
= App\Settings\ColorSettings {#8764
    +enabled: false,
    +colors: [],
    enabled: false,
    colors: [],
  }

> $s->colors = [ new ColorData(Color::White, 1, 0.9, 0.8), new ColorData(Color::Red, 1, 0.8, 1) ];
[!] Aliasing 'Color' to 'App\Enums\Color' for this Tinker session.
= [
    App\Data\Settings\Background\ColorData {#8803
      +name: App\Enums\Color {#8801
        +name: "White",
        +value: "white",
      },
      +brightness: 1.0,
      +contrast: 0.9,
      +saturation: 0.8,
    },
    App\Data\Settings\Background\ColorData {#8814
      +name: App\Enums\Color {#8820
        +name: "Red",
        +value: "red",
      },
      +brightness: 1.0,
      +contrast: 0.8,
      +saturation: 1.0,
    },
  ]
> 

> $s->save();
= App\Settings\ColorSettings {#8764
    +enabled: false,
    +colors: [
      App\Data\Settings\Background\ColorData {#8803
        +name: App\Enums\Color {#8801
          +name: "White",
          +value: "white",
        },
        +brightness: 1.0,
        +contrast: 0.9,
        +saturation: 0.8,
      },
      App\Data\Settings\Background\ColorData {#8814
        +name: App\Enums\Color {#8820
          +name: "Red",
          +value: "red",
        },
        +brightness: 1.0,
        +contrast: 0.8,
        +saturation: 1.0,
      },
    ],
    enabled: false,
    colors: [
      App\Data\Settings\Background\ColorData {#8803},
      App\Data\Settings\Background\ColorData {#8814},
    ],
  }
$ p-artisan tinker;
Psy Shell v0.11.22 (PHP 8.1.32 — cli) by Justin Hileman
> DB::table('settings')->whereGroup('background:color')->get()->toArray();
= [
    {#8784
      +"id": 2,
      +"group": "background:color",
      +"name": "colors",
      +"locked": 0,
      +"payload": "[{"name": "white", "brightness": 1, "contrast": 0.9, "saturation": 0.8}, {"name": "red", "brightness": 1, "contrast": 0.8, "saturation": 1}]",
      +"created_at": "2025-07-19 22:35:26",
      +"updated_at": "2025-07-19 22:38:26",
    },
    {#8763
      +"id": 1,
      +"group": "background:color",
      +"name": "enabled",
      +"locked": 0,
      +"payload": "false",
      +"created_at": "2025-07-19 22:35:26",
      +"updated_at": "2025-07-19 22:38:26",
    },
  ]

Best and kind regards

@serious-angel
Copy link
Contributor Author

serious-angel commented Jul 29, 2025

Since the library supports multiple arguments for casts, and considering that the cast always validates the Data passed, it feels like a second optional parameter for the Cast may be adequate, and what do you think about the following?:

lass ArrayDataCast implements SettingsCast
{
    protected string $type;

    protected bool $validate;

    public function __construct(?string $type, string $validate = 'true')
// ...

Here, it might be used as:

public static function casts(): array
{
    return [
        'colors' => ArrayDataCast::class . ':' . ColorData::class . ',false',
    ];
}

1. Added parameter for optional validation during a cast
2. Since we except the data to be arrayable, let's change the type from BaseData to Data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant