3
I’m unable to change the date format using the laravel 5.4
My model is like this:
class Feriado extends Model{
protected $fillable = ['id', 'data', 'descricao', 'created_at', 'updated_at' ];
protected $dates = ['data'=> 'm-d-Y'];
protected $primaryKey = 'id';
protected $table = 'feriados';
}
The controller that returns the data is like this:
public function index(){
$feriados= Feriado::all();
return response()->json(['error'=> false, 'mensagem'=> null, 'data'=> $feriados]);
}
And the return is coming like this:
{
"error":false,
"mensagem":null,
"data":[
{
"id":1,
"data":"2018-01-01 00:00:00",
"descricao":"Confraterniza\u00e7\u00e3o Universal",
"created_at":null,
"updated_at":null
},
{
"id":2,
"data":"2018-03-30 00:00:00",
"descricao":"Sexta-feira Santa",
"created_at":null,
"updated_at":null
},
{
"id":3,
"data":"2018-04-21 00:00:00",
"descricao":"Tiradentes",
"created_at":null,
"updated_at":null
},
{
"id":4,
"data":"2018-05-01 00:00:00",
"descricao":"Dia do Trabalhador",
"created_at":null,
"updated_at":null
},
{
"id":5,
"data":"2018-05-31 00:00:00",
"descricao":"Corpus Christi",
"created_at":null,
"updated_at":null
},
{
"id":6,
"data":"2018-09-07 00:00:00",
"descricao":"Independ\u00eancia do Brasil",
"created_at":null,
"updated_at":null
},
{
"id":7,
"data":"2018-10-12 00:00:00",
"descricao":"Padroeira do Brasil",
"created_at":null,
"updated_at":null
},
{
"id":8,
"data":"2018-02-11 00:00:00",
"descricao":"Finados",
"created_at":null,
"updated_at":null
},
{
"id":9,
"data":"2018-11-15 00:00:00",
"descricao":"Proclama\u00e7\u00e3o da Rep\u00fablica",
"created_at":null,
"updated_at":null
},
{
"id":10,
"data":"2018-12-25 00:00:00",
"descricao":"Natal",
"created_at":null,
"updated_at":null
}
]
}
The date is coming in format 2018-12-25 00:00:00
and in the model I am setting another format protected $dates = ['data'=> 'm-d-Y'];
I wonder where I’m going wrong?
To display the data on the screen I am using Angularjs, and even using a filter {{(feriado.data| date: 'dd-MM-yyyy')}}
does not display in the correct form.
https://laravel.com/docs/5.5/eloquent-serialization#appending-values-to-json read this .
– novic
I was able to define in the model no :( even after everything again...
– Milrak Pereira Pessoa
I made a reply using your model try to understand and reflect in your code the example ...
– novic