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

ioiofadhil's avatar

How properly do a Laravel $casts to nested column in mongoDB?

Hello everyone! So i jumped into a problem that defines a datetime in nested column in mongoDB Laravel.

Example in Docs

protected $casts = [
    'created_at' => 'datetime',
];

My data

$data->attribute_list = [
        "Battery" = [
            "value" = 3.98, 
            "read_time"  =  Carbon::now(), 
            "received_time"  =  Carbon::now()
], 

$data->save();

i tried to do the cast like this, but it returns null

protected $casts = [
    'attribute_list.*.read_time' => 'datetime',
    'attribute_list.*.received_time' => 'datetime',
];

How to achieve that?

0 likes
1 reply

Please or to participate in this conversation.