Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Artwork's avatar

How would you use Laravel Data to validate requests for Laravel Settings (Spatie packages)?

Dear Developers,

Thank you for the marvel!


For example, we have the following setting for Laravel Settings package by Spatie:

namespace App\Settings\General;

use App\Enums\Color;
use Spatie\LaravelSettings\Settings;

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

    public Color $name = Color::White;

    /** @var \App\Enums\ColorShade[] */
    public array $shades = [];

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

We have the package Laravel Data already installed, since it was mentioned and uncommented in the default scaffolded configuration file for Laravel Settings:

'global_casts' => [
    DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class,
    DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class,
//     Spatie\DataTransferObject\DataTransferObject::class => Spatie\LaravelSettings\SettingsCasts\DtoCast::class,
    Spatie\LaravelData\Data::class => Spatie\LaravelSettings\SettingsCasts\DataCast::class,
],

Source

Would you create a definite class for Laravel Data like the following?:

The above separate class seems interesting, but perhaps, I currently don't see a more proper way to use the setting in proper request for API with the package Laravel Data features (e.g. custom rule messages, TypeScript), and would I appreciate a hint on how to prepare a request class for the setting.


Best and kind regards

0 likes
2 replies
LaryAI's avatar
Level 58

Certainly! Here’s a recommended approach to validating Spatie Laravel Settings using spatie/laravel-data in your API requests, including custom validation messages and TypeScript support.

1. Create a Data Class for Your Settings

Yes, your approach of creating a ColorSettingsData class is correct and aligns with best practices. This class will handle validation and transformation for your settings data.

2. Use the Data Class in Your Request

You can use the data class in your controller or form request. Here’s how you might use it in a controller:

use App\Data\ColorSettingsData;
use App\Settings\General\ColorSettings;

public function updateColorSettings(Request $request)
{
    // Validate and transform the request data
    $data = ColorSettingsData::from($request);

    // Save to settings
    app(ColorSettings::class)->fill([
        'enabled' => $data->enabled,
        'name' => $data->name,
        'shades' => $data->shades,
    ])->save();

    return response()->json(['message' => 'Settings updated!']);
}

Or, if you want to use a custom Form Request:

use Spatie\LaravelData\WithData;

class UpdateColorSettingsRequest extends FormRequest
{
    use WithData;

    public function rules(): array
    {
        return ColorSettingsData::rules();
    }

    public function messages(): array
    {
        return ColorSettingsData::messages();
    }

    public function dataClass(): string
    {
        return ColorSettingsData::class;
    }
}

Then in your controller:

public function updateColorSettings(UpdateColorSettingsRequest $request)
{
    $data = $request->getData();

    app(ColorSettings::class)->fill([
        'enabled' => $data->enabled,
        'name' => $data->name,
        'shades' => $data->shades,
    ])->save();

    return response()->json(['message' => 'Settings updated!']);
}

3. TypeScript Support

If you want to generate TypeScript types from your Data class, you can use spatie/laravel-data-export:

php artisan data:export

This will generate TypeScript types for your data classes, which you can use in your frontend.


Summary:

  • Create a ColorSettingsData data class for validation and transformation.
  • Use it in your controller or a custom Form Request.
  • Optionally, generate TypeScript types for frontend use.

This approach keeps your validation, transformation, and documentation (TypeScript) in sync and leverages the full power of both Spatie packages.

Artwork's avatar

@LaryAI , this is what the Readme states, and I would not ask the question here if that would make any sense in this case.

What if we have around 20 different setting groups, and around 10 different routes around the project with 3 different APIs to manipulate the settings? Would we create 50 or more methods for each individual request class for each settings group, potentially even duplicating the requests for the same settings?

Please or to participate in this conversation.