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

Sharim's avatar

Storing Current date using Carbon::now()

I'm trying to update the date from controller. When I write dd('$data');, it stored in database, but without this the data is not saving.

if($data->status == 'DELIVERED')
            {
                if(empty($data->delivered_at))
                {
                    $date=Carbon::now();
                    
                    $data->delivered_at = $date->toDateTimeString();
                    $data->save();
                    
                }
            }
0 likes
16 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Is your delivered_at a datetime type in the database? Then just

$data->delivered_at = Carbon::now();
$data->save();

is enough. As long as the conditions above apply also so that code is executed.

Sharim's avatar

@Nakov still the same. Also I'm using

protected $casts = [
        'delivered_at' => 'datetime:Y-m-d',
    ];

I tried by removing it also. But results are same.

Sharim's avatar

@Nakov delivered_at is varchar(). Should I change it to datetime?

Nakov's avatar

@Sharim that should not be a problem. I doubt that you get into that condition at all.. Try adding dd('test'); and see if it gets printed.

Sharim's avatar

@Sinnbeck I changed it to datetime. But still when I add dd('$data') it show me correct data and time of another zone and data store. But when I remove dd('$data') page refresh without saving the date.

if($data->status == 'DELIVERED')
            {
                if(empty($data->delivered_at))
                {
                    $data->delivered_at = Carbon::now();
                    $data->save();
                    // dd($data);
                }

            }
sr57's avatar

@Sharim

same comment as above .... have a look of your code execution.

Sharim's avatar

@sr57 Yes in my method, Its a mess. Should I add this code on the last of the method?

sr57's avatar

Use 3 dump, after the save and outside the 2 if to see what your code is doing versus your test / input variables.

sr57's avatar

@Sharim

so :-)

if($data->status == 'DELIVERED')
            {
                if(empty($data->delivered_at))
                {
                    $date=Carbon::now();
                    
                    $data->delivered_at = $date->toDateTimeString();
                    $data->save();
                    dump('save');
                }'
dump('if2'');
            }
dd('if1');
Sharim's avatar

@sr57 The output is

"save"
"if2"
"if1"

But when I remove dd & dump, it's again not storing it.

Sharim's avatar

@sr57 That's how I'm using my code. I changed the place of code now it's not entering in the first if()

$data = $query->findOrFail($id);
$original_data = clone($data);

$this->insertUpdateData($request, $slug, $dataType->editRows, $data);

if($data->isDirty('status'))
        {
            if($data->status == 'DELIVERED')
            {
                if(empty($data->delivered_at))
                {
                    $date=Carbon::now();
                    
                    $data->delivered_at = $date->toDateTimeString();
                    $data->save();
                    
                }
                dd($data);
            }
            
        }

Please or to participate in this conversation.