Laravel 5 中的时间戳(updated_at、created_at)为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36998228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:20:06  来源:igfitidea点击:

Timestamps (updated_at, created_at) are null in Laravel 5

laravellaravel-5laravel-5.2

提问by el valuta

I have a problem with updated_at, created_atfields in Laravel 5.

我对Laravel 5 中的updated_at,created_at字段有问题。

Here is my migration:

这是我的迁移:

Schema::create('lots', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('lot');
    $table->integer('is_active');
    $table->timestamps();
});

But when I insert some data into this table, updated_atand created_atfields are null. How make them auto-complete with current timestamps?

但是,当我插入一些数据到这个表中,updated_at并且created_at字段为空。如何使它们使用当前时间戳自动完成?

I insert data like this:

我插入这样的数据:

\DB::table('admin_lots')->insert([
    'lot' => $request->cycle_lot,
    'is_active' => '1',
]);

Thanks.

谢谢。

回答by Alexey Mezenin

You probably do not use Eloquent when inserting data, in this case you should add timestamps manually.

你在插入数据时可能不会使用 Eloquent,在这种情况下你应该手动添加时间戳。

If you do not want to do this, but you still need filled timestamps, use this hack:

如果您不想这样做,但仍然需要填充时间戳,请使用以下技巧

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

Update

更新

Based on your updated code, here's another solution:

根据您更新的代码,这是另一种解决方案:

\DB::table('admin_lots')->insert([
                'lot'   => $request->cycle_lot,
                'is_active'     => '1',
                'created_at' = \Carbon\Carbon::now()->toDateTimeString(),
                'updated_at' = \Carbon\Carbon::now()->toDateTimeString()
            ]);

回答by Rakesh Nandi

you have to use create method instead of insert method in laravel. Create method automatically add timestamp for created_at and updated_at field. User::create(array('name' => 'John'));

你必须在 Laravel 中使用 create 方法而不是 insert 方法。Create 方法自动为 created_at 和 updated_at 字段添加时间戳。 User::create(array('name' => 'John'));

回答by Yevgeniy Afanasyev

Check if your model has this line.

检查您的模型是否有这条线。

public $timestamps = false;

If it has, delete it.

如果有,删除它。

回答by overburn

When you instert data directly, Laravel won't know about your timestamps. You can either set the timestamps manually in the insert statement, or switch to using Eloquent models , which handle many things out of the box for you, including timestamps. It's also way easier to maintain than straight queries, where applicable.

当你直接插入数据时,Laravel 不会知道你的时间戳。您可以在插入语句中手动设置时间戳,或者切换到使用 Eloquent 模型,它可以为您处理许多开箱即用的事情,包括时间戳。在适用的情况下,它也比直接查询更容易维护。

Eloquent ORM

雄辩的 ORM

回答by Saumya Rastogi

You need to use Laravel's awesome Eloquent feature to make timestamps written to the Database automatically...

你需要使用 Laravel 很棒的 Eloquent 功能来自动将时间戳写入数据库......

As by seeing your example the code for eloquent will goes like this:

通过查看您的示例,eloquent 的代码将如下所示:

$lot_inputs = array(
    'lot' => $request->cycle_lot,
    'is_active' => 1
);
$new_lot = Lot::create($lot_inputs);

Please note that you shoul have the Model for the table = 'lots' (and it must extends Eloquest) so that you can easily use Eloquent methods and its properties...

请注意,您应该拥有表的模型 = 'lots'(并且它必须扩展 Eloquest),以便您可以轻松地使用 Eloquent 方法及其属性...

It would be great if you use Eloquent ORMas much as possible so that if in future you want to change your DB technology then you won't need to specify the written eloquent queries again (e.g: the conversion of query to different DB languages is automatically done by Eloquent)

如果你尽可能多地使用Eloquent ORM那就太好了,这样以后如果你想改变你的数据库技术,那么你就不需要再次指定编写的 eloquent 查询(例如:将查询转换为不同的数据库语言是由 Eloquent 自动完成)

Thanks I hope this will help you to resolve your issue..!!

谢谢我希望这会帮助你解决你的问题..!!