Typically, the Laravel platform have a $table->timestamps();
in migration..., it generate two datetime
fields,
But I would like to implement my own timestamps or, maybe call unix_timestamps()
. I would like to have two fields named created_at
, and updated_at
, and which store the unix timestamps, how can I implement it? Thanks.
You don't have to use Laravel's timestamp helpers, but they are convenient. There are some good ways of working with string timestamps now too, including PHP's DateTime class. But I digress, to use unix timestamps...
In your Schema (migration), use
$table->integer('created_at');
$table->integer('updated_at');
instead of
$table->timestamps();
Replace the timestamp()
function in your models.
$timestamps = true
in your models.Here's an example base model which you could use, and extend instead of Eloquent on your models:
// models/basemodel.php
class BaseModel extends Eloquent {
/**
* Indicates if the model has update and creation timestamps.
*
* @var bool
*/
public static $timestamps = true;
/**
* Set the update and creation timestamps on the model.
*/
public function timestamp()
{
$this->updated_at = time();
if ( ! $this->exists) $this->created_at = $this->updated_at;
}
}
// models/thing.php
class Thing extends BaseModel {
}