php - How to make my own timestamp method in Laravel? -
typically, laravel platform have $table->timestamps();
in migration..., generate 2 datetime
fields, implement own timestamps or, maybe call unix_timestamps()
. have 2 fields named created_at
, , updated_at
, , store unix timestamps, how can implement it? thanks.
you don't have use laravel's timestamp helpers, convenient. there ways of working string timestamps too, including php's datetime class. digress, use unix timestamps...
in schema (migration), use
$table->integer('created_at'); $table->integer('updated_at');
instead of
$table->timestamps();
replace
timestamp()
function in models.- keep
$timestamps = true
in models.
here's example base model use, , extend instead of eloquent on models:
// models/basemodel.php class basemodel extends eloquent { /** * indicates if model has update , creation timestamps. * * @var bool */ public static $timestamps = true; /** * set update , creation timestamps on model. */ public function timestamp() { $this->updated_at = time(); if ( ! $this->exists) $this->created_at = $this->updated_at; } } // models/thing.php class thing extends basemodel { }
Comments
Post a Comment