Home > Enterprise >  why carbon::now() does not work on observer or job in host (laravel)?
why carbon::now() does not work on observer or job in host (laravel)?

Time:09-19

I set a field of type timestamp with carbon::now(), but field get null value!

in the job (handle() method) or observer (created() method) :

$cart->publish_date = Carbon::now();
$cart->save();

but in the localhost everything is ok !

what is wrong ?

thanks

CodePudding user response:

Carbon::now(); returns an object of Carbon. If you need to save it to a database column you must do:

$cart->publish_date = Carbon::now()->toDateTimeString();
$cart->save();

CodePudding user response:

You could give this a try.

$cart->update(['publish_date' => now()]);

This reads a bit cleaner IMO and removes the need for the Carbon import (If Carbon isn't being used anywhere else in the class)

  • Related