You can directly format the date with your casts .
Like this .
protected function casts(): array
{
return [
'depot' => 'datetime:Y-m-d H:i:s',
];
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I migrate from Laravel 11 to Laravel 12 in the start of this year, and now i got a problem with the casts function on a model and not on another. For the first model I have this :
protected function casts(): array
{
return [
'depot' => 'datetime',
];
}
I got with the API the datetime like that : "2025-04-08T15:46:39.000000Z"
For the second model which is users I have this :
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'validation_parcel' => 'datetime',
];
}
It was ok in Laravel 11, but in Laravel 12 I get this now : "2025-04-08 15:46:39" I don't understand what is wrong with my code.
Thanks for your help.
Regards
@FabienArr Ah, this explains a lot. I think you should refactor your constructor to match parent's constructor (parent is \Illuminate\Database\Eloquent\Model class, there is some magic in it's constructor, you can check inside file vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php) and include parent::__construct(...):
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
if (date('m') >= 8) { // A partir du 1er Aout année n
$this->campagne = date('Y');
} else { // Jusqu'au 31 juillet année n-1
$this->campagne = date('Y') - 1;
}
}
Can you try it?
Please or to participate in this conversation.