skip to Main Content

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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. The best way is to use mutators in laravel, and also using the Carbon package.

    It should be implemented like this:

    Employee.php

    use CarbonCarbon;
    
    class Employee extends Model {
    
    public function setHireDateAttribute($value)
    {
        $carbonDate = Carbon::create($value)
        $this->attributes['hire_date']->toDateString();
    }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search