0

When I Retrieve date from SQL database in my laptop only it back to me like (May 17 2016 12:00:00:AM) but any server or another laptop back like (2016-05-17), it appears when I call date column from database and I think this issue not from code I think in PHP or apache2 in my laptop.

This is my code:

$user_data = User::with('employee')
    ->where('id', Auth::user()->id)
    ->get()
    ->first()
    ->toArray();

dd($user_data['employee']['hire_date']);

enter image description here

Rwd
  • 34,180
  • 6
  • 64
  • 78

2 Answers2

0

The reason this is occuring is likely to be because on your laptop you have a DATETIME field in the database and on the server you have a DATE field. These field types are fundamentally different.

bgld
  • 31
  • 5
0

The best way is to use mutators in laravel, and also using the Carbon package.

It should be implemented like this:

Employee.php

use Carbon\Carbon;

class Employee extends Model {

public function setHireDateAttribute($value)
{
    $carbonDate = Carbon::create($value)
    $this->attributes['hire_date']->toDateString();
}

}
Majid Alaeinia
  • 962
  • 2
  • 11
  • 27