Formatting date with Laravel

3

I can not change the date format using laravel 5.4

My model looks 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 looks 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 2018-12-25 00:00:00 format and in the model I'm setting another format protected $dates = ['data'=> 'm-d-Y'];

Where am I going wrong?

To display the data on the screen I'm using angularjs, and even using a {{(feriado.data| date: 'dd-MM-yyyy')}} f filter does not display correctly.

    
asked by anonymous 27.10.2017 / 04:06

4 answers

1

Do not change anything in your model in the default question, just make a new field that will be shown from the results of it, if you want a date in the specific pattern, Create a method that will be appended to the returned values of a query, either array or json , a minimum example :

Set your Model :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Feriado extends Model
{
    protected $primaryKey = 'id';
    protected $table = 'feriado';
    protected $fillable = ['id', 'data', 'descricao'];
    protected $dates = ['created_at','updated_at'];

    //data formatada m-d-Y
    protected $appends = ['data2'];
    public function getData2Attribute()
    {
        return date('m-d-Y', strtotime($this->attributes['data']));
    }

}

This setting $appends will only serve to show data, it has no relation to the fields that relate to the table, but use them to show certain types of values and formats. The getData2Attribute() method has the responsibility of doing this conversion and displaying the formatted date (which can be of any format, depends on the established rule), and everything is independent of the table pattern in the database is this useful for maintaining operations without conversion field, this field that was appended is only used when to show information in methods, eg:

>>> App\Models\Feriado::all()->toArray();    
=> [                                         
     [                                       
       "id" => 1,                            
       "data" => "2016-10-09",               
       "descricao" => "Festival",            
       "created_at" => "2017-10-27 00:00:00",
       "updated_at" => null,                 
       "data2" => "10-09-2016",              
     ],                                      
     [                                       
       "id" => 2,                            
       "data" => "2016-11-11",               
       "descricao" => "Carnaval",            
       "created_at" => "2017-10-27 00:00:00",
       "updated_at" => null,                 
       "data2" => "11-11-2016",              
     ],                                      
   ]                                         
>>> 

In this example the enclosed field data2 is in the expected format. There is a confusion in Mutators that has the purpose of changing the information retrieval characteristic of Model , which in many cases breaks the logic and gives many problems if it is done in the wrong way. It is widely used in Date or DateTime fields (but in any field there may be an employability), but, you must understand that at all times you must use the conventions of the class and format the data and to update your information must be an instance of the class DateTime , very different from the first example that is the best solution for you.

References:

27.10.2017 / 17:33
0

Following the Laravel documentation link

Change this:

protected $dates = ['data'=> 'm-d-Y'];

To:

protected $dates = [
    'data'
];

And add this line:

protected $dateFormat = 'm-d-Y';
    
27.10.2017 / 15:00
0

Your bank's date format is Y-m-d, which is the default, use function $ holidays-> date-> format ('d / m / Y') in your view.

    
27.10.2017 / 15:21
0

Try the solution of David Santos, it should work, but an alternative if not right, would make the change in the hand, is a gambiarra but it works:

public function index(){
    $feriados= Feriado::all();
    foreach($feriados as $feriado){
        $feriado->data = date("d-m-Y", strtotime($feriado->data));
    }
    return response()->json(['error'=> false, 'mensagem'=> null, 'data'=> $feriados]);
}
    
27.10.2017 / 15:27