通常,Laravel平台在迁移中有一个$table->timestamps();
...,它会生成两个datetime
字段,但是我想实现我自己的时间戳,或者可能会调用unix_timestamps()
。我想有两个字段叫做created_at
和updated_at
,哪个存储unix时间戳,我该如何实现呢?谢谢。如何在Laravel中创建自己的时间戳方法?
回答
您不必使用Laravel的时间戳帮助程序,但它们很方便。现在还有一些使用字符串时间戳的好方法,包括PHP的DateTime类。但是,我离题,使用UNIX时间戳...
在你的架构(迁移),使用
$table->integer('created_at'); $table->integer('updated_at');
,而不是
$table->timestamps();
在你的模型替换
timestamp()
功能。- 将
$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 {
}
对于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');
});
}
此方法适用于我,但是,在相关表中,我必须禁用“touches”属性,因为运行db:seed时发生InvalidArgumentException。我没有时间仔细研究目前的原因。 – 2014-02-03 22:14:15
我有同样的要求,想通了,可能会为你工作了一个解决方案。我贴的我是如何做到的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'];
}
谢谢,这工作就像一个魅力! – Jurgen 2014-11-19 08:30:04
只需创建一个时间戳类型迁移/示范田,那么在你的控制器中,用当前时间填充它使用这个
$myField = new \DateTime();
- 1. 如何在laravel中设置我的自定义时间戳?
- 2. 使用unix_timestamp方法在spark中创建时间戳
- 3. 在postgres中创建列的时间戳
- 4. 创建自己的Array#地图方法
- 5. 在Jenkinsfile中创建一个时间戳
- 6. 在PostgreSQL中创建默认时间戳
- 7. 创建自己的自定义适配器时,getView()方法如何工作?
- 8. 便携式在c/C++中创建时间戳的方式
- 9. PHP UTC时间戳创建
- 10. Pyspark创建时间戳列
- 11. 在java中创建我自己的集合的正确方法
- 12. 如何在Play中创建自动生成的日期/时间戳字段!/JPA?
- 13. 如何在MySQL表中创建2个自动设置的时间戳列?
- 14. 如何创建自己的atan(反正切)方法?
- 15. Laravel创建方法
- 16. Laravel ::创建方法
- 17. 在Laravel中创建方法的问题
- 18. 在onLocationChanged方法中设置时间戳
- 19. 如何在Java中为AES算法创建自己的密钥?
- 20. laravel 5.4,如何插入时间戳(mysql)
- 21. Laravel 3:我如何创建我自己的助手
- 22. 如何在PHP中创建SQLite时间戳值
- 23. 如何使用firestore TIMESTAMP在JavaScript中创建时间戳
- 24. 如何在TCL中创建RFC 3339时间戳?
- 25. 如何从自己获取最后退出时间戳?
- 26. 何时创建您自己的IEnumerable类
- 27. 创建不带时区的时间戳
- 28. 创建时间戳为NSNumber在objective-c
- 29. MySQL现在的方法vs时间戳
- 30. R:如何从时间戳创建连续的日期间隔
我假设你想要与$ table-> timestamp()相同的功能,即自动更新时间戳?否则$ table-> integer('created_at')应该可以工作。 – 2013-04-05 12:36:28