Skip to content
Advertisement

How to get Date & Time from Database table column in laravel?

I’m trying to store and get current date & time from Database. I’m trying this

  $current_time    = Carbon::now(); 
  'order_date_and_time'    => $current_time,
  'delevery_date_and_time' => $current_time->addDays(3), 

Here is my migration

   $table->dateTime('order_date_and_time');
   $table->dateTime('delevery_date_and_time');

I store current date & time perfectly but I couldn’t get date & time. I use this method to get DateTime

      toDateTimeString()

I got this error

 "Call to a member function toDateTimeString() on string"

Can anyone tell me how can I get Date & Time ?

Advertisement

Answer

Add order_date_and_time and delevery_date_and_time to $dates in your model. It will cast the attributes to Carbon instance as said in laravel doc

protected $dates=[
    'order_date_and_time',
    'delevery_date_and_time',
];
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement