Hi guys. I don't know if this is intended behaviour of laravel or not, but when I have casts such as this (note the dateFormat)
protected $casts = [
'time_date' => 'datetime:Y-m-d',
'time_enddate' => 'datetime',
];
protected $dateFormat = "U"; // unix timestamp
On some model Time, then if I do
$time = new Time;
$time->time_date = now();
$time->time_enddate = now();
dd($time);
I see the following printed in the dump and die
#attributes: array:3 [▼
"time_date" => Illuminate\Support\Carbon @1645374270 {#1844 ▶}
"time_enddate" => "1645374270"
]
This to me is unexpected behaviour. The way that a variable should be cast should have no impact on the way it is set. Furthermore, this behaviour is not in line with what I read in the laravel docs.
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
];
When a column is cast as a date, you may set the corresponding model attribute value to a UNIX timestamp, date string (Y-m-d), date-time string, or a DateTime / Carbon instance. The date's value will be correctly converted and stored in your database.
Can someone explain why this happens?