DateTime Error when using 'Spatie Settings' plugin

I'm getting this error when I try to edit the settings for my site Object of class DateTime could not be converted to string - Using https://filamentphp.com/plugins/filament-spatie-settings ManageCompetitions.php page
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\ColorPicker::make('color')
->required()
->label('Competition Color'),
Forms\Components\DateTimePicker::make('start_date')
->required()
->label('Competition Start'),
Forms\Components\DateTimePicker::make('end_date')
->required()
->label('Competition End'),
Forms\Components\TextInput::make('name')
->required(),
Forms\Components\ColorPicker::make('color')
->required()
->label('Competition Color'),
Forms\Components\DateTimePicker::make('start_date')
->required()
->label('Competition Start'),
Forms\Components\DateTimePicker::make('end_date')
->required()
->label('Competition End'),
CompetitionSettings.php page
<?php

namespace App\Settings;

use DateTime;
use Spatie\LaravelSettings\Settings;
use Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast;

class CompetitionSettings extends Settings
{
public string $name;
public DateTime $start_date;
public DateTime $end_date;
public string $color;
public string $image;
public ?string $scoreboard;
public bool $has_aws_creds;
public bool $has_bifrost;

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

public static function casts(): array
{
return [
'start_date' => DateTimeInterfaceCast::class,
'end_date' => DateTimeInterfaceCast::class,
];
}
}
<?php

namespace App\Settings;

use DateTime;
use Spatie\LaravelSettings\Settings;
use Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast;

class CompetitionSettings extends Settings
{
public string $name;
public DateTime $start_date;
public DateTime $end_date;
public string $color;
public string $image;
public ?string $scoreboard;
public bool $has_aws_creds;
public bool $has_bifrost;

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

public static function casts(): array
{
return [
'start_date' => DateTimeInterfaceCast::class,
'end_date' => DateTimeInterfaceCast::class,
];
}
}
15 Replies
awcodes
awcodes5mo ago
What is the actual error?
GodSekhmet
GodSekhmetOP5mo ago
No description
awcodes
awcodes5mo ago
Would need to see the stack trace, but my guess is that the filament fields are expecting a Carbon instance instead.
GodSekhmet
GodSekhmetOP5mo ago
Here's the stack trace - I'll switch it to a carbon instance and see if it fixes it.
GodSekhmet
GodSekhmetOP5mo ago
I switched the typehint to Carbon\Carbon which allowed the page to load, but it's erroring on save now.
awcodes
awcodes5mo ago
Hmm, sorry, don’t have a lot of experience with the spatie’s package. But definitely a type cast mismatch happening somewhere. Do you have a repo you could share?
awcodes
awcodes5mo ago
Try CabonInterface in stead of Carbon. In the CompetitionSettings class
GodSekhmet
GodSekhmetOP5mo ago
but this might be in the spatie settings, let me see if I can fix it
No description
awcodes
awcodes5mo ago
Hmm, yea. Tough to say, since i don’t have more experience with the spatie package. Sorry.
GodSekhmet
GodSekhmetOP5mo ago
No worries, Thank you for the help!
awcodes
awcodes5mo ago
Definitely think it’s a casting issue though. Just not sure where it’s conflicting. Don’t think it’s a filament issue though.
GodSekhmet
GodSekhmetOP5mo ago
Agreed, definitely don't think it's a filament issue. Just not sure how to fix it I'll open an issue with Spatie!
awcodes
awcodes5mo ago
My advice would be to try dd’ing along the stack trace to see where it’s failing specifically.
GodSekhmet
GodSekhmetOP5mo ago
Oh great idea, Thank you!

Did you find this page helpful?