Laravel时间戳()不会创建CURRENT_TIMESTAMP [英] Laravel timestamps() doesn't create CURRENT_TIMESTAMP

查看:1794
本文介绍了Laravel时间戳()不会创建CURRENT_TIMESTAMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 timestamps()方法的迁移,然后我有一个种子种子这个表。

  Schema :: create('mytable',function(Blueprint $ table){
$ table-> incremental('id');
$ table-> string('title');
$ table-> timestamps();
});

种子如下所示:

  DB :: table('mytable') - > insert([
[
'title'=>'我的Awesome标题'
]
]);

当它全部运行时使用:

  php artisan migrate:refresh --seed 

插入,但 created_at updated_at 的值均为 0000-00-00 00: 00:00 为什么他们设置不正确?



这是它创建的列方案:

 `created_at` TIMESTAMP NOT NULL DEFAULT'0000-00-00 00:00:00',
`updated_at` TIMESTAMP NOT NULL DEFAULT'0000-00 -00 00:00:00',

我想要这些方案:

 `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,


解决方案

当您插入数据不使用Eloquent时,您需要自己插入时间戳。 p>

如果您使用:

  $ x = new MyTable(); 
$ x-> title ='My Awesome Title';
$ x-> save();

您将正确填写时间戳(当然您需要创建 MyTable 模型第一)



编辑



如果你真的想要它可以改变:

  $ table-> timestamps(); 

into:

 code> $表 - >时间戳( 'created_at') - >默认(\DB ::原始( 'CURRENT_TIMESTAMP')); 
$ table-> timestamp('updated_at') - > default(\DB :: raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

如果您为此表创建模型,则应设置

  $ timestamps = false; 

以确保雄辩不会尝试设置他们。



EDIT2



还有一个更重要的问题。如果您将PHP中的设置日期与MySQL中的其他设置日期混合在一起,您应该确保在PHP和MySQL中都有完全相同的datetime(和时区),或者您应该使用与记录中设置的相同的日期比较(MySQL或PHP)。否则,运行查询时可能会得到意想不到的结果,例如

  SELECT * FROM mytable WHERE DATE(created_at)= CURDATE()

可能与传递PHP日期的运行查询不同

 SELECT * FROM mytable WHERE DATE(created_at)='.date('Ymd)。'

因为在PHP服务器上可能是例如2015-12-29,但在MySQL服务器上2015-12-30


I have a migration that has the timestamps() method, and then I have a seed to seed this table.

Schema::create('mytable', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

The seed looks like this:

DB::table('mytable')->insert([
    [
        'title' => 'My Awesome Title'
    ]
]);

When it all gets run using:

php artisan migrate:refresh --seed

The item gets inserted, but the values of created_at and updated_at are both 0000-00-00 00:00:00 why are they not set correctly?

here are the column schemes that it creates:

`created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',

I would like these schemes:

`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

解决方案

When you insert data not using Eloquent you need to insert timestamps on your own.

If you use:

$x = new MyTable();
$x->title = 'My Awesome Title';
$x->save();

you will have timestamp filled correctly (of course you need to create MyTable model first)

EDIT

If you really want it you can change:

$table->timestamps();

into:

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

And if you create model for this table, you should set

$timestamps = false;

to make sure Eloquent won't try to set them on his way.

EDIT2

There is also one more important issue. In case you mix setting dates in tables from PHP and in other in MySQL you should make sure that both in both PHP and MySQL there's exact same datetime (and timezone) or you should use the same date comparison as you set in record (either MySQL or PHP). Otherwise when running queries you might get unexpected results for example

SELECT * FROM mytable WHERE DATE(created_at) = CURDATE()

might be different than running query with passing PHP date

"SELECT * FROM mytable WHERE DATE(created_at) = '".date('Y-m-d")."'"

because on PHP server it might be for example 2015-12-29 but on MySQL server 2015-12-30

这篇关于Laravel时间戳()不会创建CURRENT_TIMESTAMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆