Home > Software design >  Could not parse '[{"created_at":"2021-11-20T15:14:28.000000Z"}]'. How
Could not parse '[{"created_at":"2021-11-20T15:14:28.000000Z"}]'. How

Time:11-22

error: Could not parse '[{"created_at":"2021-11-20T15:14:28.000000Z"}]': DateTime::__construct(): Failed to parse time string ([{"created_at":"2021-11-20T15:14:28.000000Z"}]) at position 0 ([): Unexpected character

Controller :

$created_at = $ar->where('status', 0)->get('created_at');  
$backlog = Carbon::parse($created_at)->format('y-m-d');

i was trying to get the auto generated created_at in my database and add 3 days on it and so far i only saw the carbon way which is to parse it with carbon and then use the addDays() but unfortunately the system sees the whole column name and time. How can i parse an auto generated timestamp?

CodePudding user response:

because you need to decode it first:

  $value= json_decode('[{"created_at":"2021-11-20T15:14:28.000000Z"}]')[0]->created_at;
  $parsedValue=Carbon::parse($value);

then you can use $parsedValue in your query as usual

CodePudding user response:

You can get a specific the column from the first row of a result set using the value query builder method. In your case:

$backlog = $ar->where('status', 0)->value('created_at');  

If your model is properly defined this should already be a Carbon instance or null if there are no results in the result set.

  • Related