1
votes

According to Laravel Docs that says :

By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'U';
}

I want to store all created_at and updated_at values as integer unix timestamp.

Which values should I set for protected $dateFormat variable without using Date Mutators ?

2

2 Answers

0
votes

I don't know why the documentation tells to set dateFormat as a property, but this works:

protected function getDateFormat()
{
    return 'U';
}
0
votes

I thought that default value of dateFormat property is U that cause all dateTime values stored as String TimeStamp (YYYY-MM-DD HH:MM:SS) while when I want to Store as integer timeStamp, must to add protected $dateFormat = 'U'; to desired Model. Sorry for this Careless.