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();
}
}
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.
@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.
@Nakov delivered_at is varchar(). Should I change it to datetime?
@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 Why varchar? I think even sqlite supports datetime
@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);
}
}
@Sharim
same comment as above .... have a look of your code execution.
@sr57 Yes in my method, Its a mess. Should I add this code on the last of the method?
Use 3 dump, after the save and outside the 2 if to see what your code is doing versus your test / input variables.
@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');
@sr57 The output is
"save"
"if2"
"if1"
But when I remove dd & dump, it's again not storing it.
@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);
}
}
@Sharim
and? ... Can you close this thread?
Please sign in or create an account to participate in this conversation.