How to make my own timestamp method in Laravel?

Typically, the Laravel platform has $table->timestamps(); in migration ..., it generates 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 that store unix timestamps, how can I implement it? Thank you

+4
source share
5 answers

You do not need to use the Laravel temporary stamp helpers, but they are convenient. There are several good ways to work with string timestamps, including the PHP DateTime class. But I digress to use unix timestamps ...

  • In your scheme (migration) use

     $table->integer('created_at'); $table->integer('updated_at'); 

    instead

     $table->timestamps(); 
  • Replace the timestamp() function in your models.

  • Keep $timestamps = true in your models.

Here is an example of a basic model that 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 { } 
+7
source

For Laravel 4:

  • override the freshTimestamp () method in your Eloquent model
  • use integers in migration file instead of timestamp

models /product.php

 class Product extends Eloquent { protected $table = 'products'; public function freshTimestamp() { return time(); } } 

Laravel 4 also mutates all dates / timestamps for Carbon instances (Documented here )

This means that you also need to overwrite the getDates() method to prevent carbon destroying your timestamp before inserting.

 public function getDates() { return array(); } 

database / migration / 2013_04_20_125823_create_products_table.php :

 public function up() { Schema::create('products', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('created_at'); $table->integer('updated_at'); }); } 
+4
source

I'm afraid you will need some ugly hacks to rewrite the timestamps() function, and I'm sure this is a bad idea.

If you need your own format, just define a new column. There is even a time column in the Laravel schema builder (see here for a complete list of available formats):

 $table->timestamp('added_on'); 

However, you will need to determine the default values ​​and / or ON UPDATE yourself, or you can use triggers . But in the end, you probably stick with Laravel timestamps() best because it will take care of everything automatically. Why do you need anything else?

+3
source

I had the same requirement, and I found a solution that might work for you too. I posted a repo on how I did this on Github: Laravel Integer SQL Dates <== check it out for more details, but here from this:

 class Base extends Eloquent { public function freshTimestamp() { return time(); // (int) instead of '2000-00-00 00:00:00' } public function fromDateTime($value) { return $value; // Don't mutate our (int) on INSERT! } // Uncomment, if you don't want Carbon API on SELECTs // protected function asDateTime($value) // { // return $value; // } public function getDateFormat() { return 'U'; // PHP date() Seconds since the Unix Epoch } } class User extends Base { protected $table = 'users'; protected $fillable = ['email']; } 
+2
source

Just create a migration / model field, which is the type of label, then in your controller, fill it with the current time using this

 $myField = new \DateTime(); 
+1
source

All Articles