mardi 11 octobre 2016

Laravel change Carbon format in toJSON

I have a model called PurchasingFactoryDate, with the following:

protected $dates = ['milestone_date'];

This is so that I can easily format the date in the view. Normal stuff here.

However, when I am trying to give a JSON response, I want this date to be formatted. I have tried the following:

$fdate->milestone_date = $fdate->milestone_date->format('d-m-Y');

However I got an error, with this important line:

at Carbon::createFromFormat('Y-m-d H:i:s', '19-10-2016') in Model.php line 2850

So Laravel thinks that I am trying to set a new date for my model, and is therefore parsing it as though it is in the above format. THis method appears not to work.

However, I have also tried serialising the model to an array, and editing in that way, but this doesn't work for me as I need to call some Model methods before I handle the date (can't really show the code for that, but trust me).

Is there a way, then, to upon calling $collection->toJSON() and returning the response, that I can get laravel the convert the date into a desired format? It would make most sense, I guess, to do it at this point.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire