vendredi 3 juillet 2020

Protected dates returns -0001-11-30 as date on API

So here is my migration file input where I get all "0000-00-00" by default:

$table->date('birthday')->default('0000-00-00')->after('email'); // Pass in the field to the DB as date
$table->date('hire_date')->default('0000-00-00')->after('birthday'); // Pass in the field to the DB as date

enter image description here


Once I run my CSV import, the DB returns results as shown below (All blank fields imports from the CSV get kept as "0000-00-00" in the DB which is what I want (This works):

enter image description here


Problem: On the API output, this is where things get corrupt - I get a DateTime object returned but all items in the DB that have "0000-00-00" get returned as this response:

"birthday": {
    "date": "-0001-11-30 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "UTC"
},
"hire_date": {
    "date": "-0001-11-30 00:00:00.000000",
    "timezone_type": 3,
    "timezone": "UTC"
},

My API method:

public function transform($employee)
{
    return [
        'birthday' => $employee['birthday'],
        'hire_date' => $employee['hire_date'],
    ];
}

Possible problem: When I define the two params in the protected $dates that extends Model I get this response, as soon as I remove the two items, then I get a "0000-00-00" response on the API.

How do I have the two items defined as dates, but also geet a "0000-00-00" response on the API?

protected $dates = [
    'birthday',
    'hire_date', // Removing the following items from dates, returns as string
    'created_date',
    'updated_date',
    'touched_at',
    'deleted_at',
];


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire