2013-04-05 128 views
4

通常,Laravel平台在迁移中有一个$table->timestamps(); ...,它会生成两个datetime字段,但是我想实现我自己的时间戳,或者可能会调用unix_timestamps()。我想有两个字段叫做created_atupdated_at,哪个存储unix时间戳,我该如何实现呢?谢谢。如何在Laravel中创建自己的时间戳方法?

+0

我假设你想要与$ table-> timestamp()相同的功能,即自动更新时间戳?否则$ table-> integer('created_at')应该可以工作。 – 2013-04-05 12:36:28

回答

7

您不必使用Laravel的时间戳帮助程序,但它们很方便。现在还有一些使用字符串时间戳的好方法,包括PHP的DateTime类。但是,我离题,使用UNIX时间戳...

  1. 在你的架构(迁移),使用

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

    ,而不是

    $table->timestamps(); 
    
  2. 在你的模型替换timestamp()功能。

  3. $timestamps = true保留在您的模型中。

下面是一个例子基础模型,你可以使用,并延长,而不是雄辩您的模型:

// 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 { 

} 
3

我担心你需要一些丑陋的黑客来重写timestamps()函数,我敢肯定这是一个坏主意。

如果您需要自己的格式,只需定义一个新列。甚至还有在Laravel的架构构建一个时间戳列(见here可用格式的完整列表):

$table->timestamp('added_on'); 

但是,您将需要自己定义默认值和/或ON UPDATE,或者你可以使用triggers。但最终你最好坚持Laravel的timestamps(),因为它会自动处理所有事情。你为什么还需要别的东西?

4

对于Laravel 4:

  • 覆盖在freshTimestamp()方法你雄辩模型
  • 在迁移文件中使用整数代替时间戳

型号/ product.php

class Product extends Eloquent { 
    protected $table = 'products'; 

    public function freshTimestamp() 
    { 
     return time(); 
    } 
} 

Laravel 4也发生变异的所有日期/时间戳碳实例(记录在案here

这意味着你还需要覆盖getDates()方法,以防止Carbon在插入前破坏您的时间戳。

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

数据库/迁移/ 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'); 
    }); 
} 
+0

此方法适用于我,但是,在相关表中,我必须禁用“touches”属性,因为运行db:seed时发生InvalidArgumentException。我没有时间仔细研究目前的原因。 – 2014-02-03 22:14:15

2

我有同样的要求,想通了,可能会为你工作了一个解决方案。我贴的我是如何做到的Github上回购:Laravel Integer SQL Dates < ==检查出来的更多的细节,但这里是它的要点是:

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']; 
} 
+1

谢谢,这工作就像一个魅力! – Jurgen 2014-11-19 08:30:04

1

只需创建一个时间戳类型迁移/示范田,那么在你的控制器中,用当前时间填充它使用这个

$myField = new \DateTime();