If any of your settings classes declare a property typed DateTimeInterface or CarbonInterface, this release removes a landmine that existed in 3.10.1. The global cast configuration happily assigned DateTimeInterfaceCast to those properties, but the cast itself only knew how to hydrate a fixed list of concrete date classes. Reading the settings back threw an exception, which effectively made the entire settings class unusable, since settings are hydrated the moment they are resolved from the container. The cast now resolves interface-typed dates through Laravel's date factory — the same mechanism Eloquent's date casts use — so these properties load correctly and respect whatever date class your app registered with Date::use(). There are no schema changes, no migration steps, and concretely typed date properties behave exactly as before.
What was broken
SettingsCastFactory::shouldCast() assigns the default DateTimeInterfaceCast not only to properties typed DateTimeInterface, but to any property typed with an interface that extends it. Since Carbon\CarbonInterface extends DateTimeInterface, both of these declarations were wired up to the cast:
class AppSettings extends Settings
{
public DateTimeInterface $launched_at;
public ?CarbonInterface $deadline;
public static function group(): string
{
return 'app';
}
}
Saving such settings worked fine — the cast's set() side serializes any DateTimeInterface implementation to a string. The problem was reading them back. DateTimeInterfaceCast::get() matched against a hardcoded list of concrete classes (DateTime, DateTimeImmutable, Carbon, CarbonImmutable, and Illuminate\Support\Carbon), and neither interface appeared in it. Hydration failed with:
Could not cast DateTime type `Carbon\CarbonInterface`
Because repositories load all settings as soon as the class is resolved, a single interface-typed property took down the whole settings class.
The fix: delegate interface types to the date factory
DateTimeInterfaceCast::get() now recognizes the two interface types and builds the value with Laravel's Date facade:
if (in_array($this->type, [DateTimeInterface::class, CarbonInterface::class], true)) {
return Date::parse($payload);
}
This mirrors how Eloquent casts date and datetime attributes, which makes settings and models consistent: by default an interface-typed property hydrates to an Illuminate\Support\Carbon instance, and if your application calls Date::use(CarbonImmutable::class) it becomes a Carbon\CarbonImmutable. Both classes implement DateTimeInterface and CarbonInterface, so the property's declared type is satisfied either way. A null payload still hydrates to null, so optional properties keep working.
A full round trip with a custom date factory now looks like this:
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
Date::use(CarbonImmutable::class);
$settings->launched_at = new DateTimeImmutable('2027-02-03T14:15:16-05:00');
$settings->save(); // stored as '2027-02-03T14:15:16-05:00'
$settings->refresh();
$settings->launched_at->format(DATE_ATOM); // '2027-02-03T14:15:16-05:00'
$settings->launched_at::class; // Carbon\CarbonImmutable
Concrete date types are untouched
If a property is typed with a concrete class — DateTime, DateTimeImmutable, Carbon, CarbonImmutable, or Illuminate\Support\Carbon — the cast still constructs exactly that class, regardless of the date factory. Calling Date::use(CarbonImmutable::class) app-wide will not sneak an immutable date into a property you declared as Carbon. If you declare a concrete type, you get exactly that type back, and the set() side still serializes it to the same string format as before. Anything that is not a supported date type or interface still throws the same exception as before.
What this means for your upgrade
For most projects, this is a pure bug fix with zero action required:
- If you never typed a settings property with
DateTimeInterfaceorCarbonInterface, nothing changes for you. - If a settings class previously failed to load with
Could not cast DateTime type …, it will hydrate normally after upgrading. No stored values need rewriting; they were already saved as plain date strings. - If you worked around the bug by typing properties concretely or by registering a custom cast for
CarbonInterfacein your settings class'scasts(), you can keep that workaround — per-class casts still take precedence — or clean it up and switch back to the interface type. - One behavioral nuance to be aware of: an interface-typed property now returns whatever the configured date factory produces. If you depend on a specific concrete class (for example, you call mutable-only methods on it), declare the property with that concrete class instead of the interface.
Takeaways
- Settings properties typed
DateTimeInterfaceorCarbonInterfaceno longer throw when loaded. - Interface-typed dates are resolved with
Date::parse(), matching Eloquent's date casts and honoringDate::use(). - Concrete date types (
DateTime,DateTimeImmutable,Carbon,CarbonImmutable,Illuminate\Support\Carbon) are cast exactly as before. - No migrations or code changes are required to upgrade; existing workarounds can optionally be removed.
- When you need a guaranteed concrete class, type the property with that class rather than the interface.