Laravel 播种导致空时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42639976/
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-14 15:29:18  来源:igfitidea点击:

Laravel seeding results in Null timestamp

laraveleloquentlaravel-5.4laravel-seeding

提问by Jhivan

So i had a seeder for Languages Table (LanguageTableSeeder) as follows:

所以我有一个语言表 ( LanguageTableSeeder)的播种机,如下所示:

DB::table('languages')->insert([
    'name'        => 'English',
    'flag'        => '',
    'abbr'        => 'en',
    'script'    => 'Latn',
    'native'    => 'English',
    'active'    => '1',
    'default'    => '1',
]);

    $this->command->info('Language seeding successful.');

But this resulted in created_atand updated_atfields to be Null in database. i looked up the pre shipped UsersTabeSeederand changed my LanguageTableSeederto mach the exact same format:

但是,这导致created_atupdated_at领域是空的数据库。我查找了预装货UsersTabeSeeder并将我的更改LanguageTableSeeder为完全相同的格式:

DB::table('languages')->delete();

$languages = [
    [
        'name' => 'English',
        'flag' => '',
        'abbr' => 'en',
        'script' => 'Latn',
        'native' => 'English',
        'active' => '1',
        'default' => '1',
    ],
];

foreach ($languages as $language){
    Language::create($language);
}

This also resulted in created_atand updated_atfields to be Null which is odd because when i look up at Users Table in my data base they have created_atand updated_atfields to be set at the exact time of running the seeder.

这也造成了created_atupdated_at字段为空这是奇怪的,因为当我在用户表中我的数据查找基础,这些created_atupdated_at领域在运行播种机的确切时间进行设置。

So here is my question. Why is this happening? and is it necessary to use:

所以这是我的问题。为什么会这样?是否有必要使用:

'created_at' => date("Y-m-d H:i:s"),
'updated_at' => date("Y-m-d H:i:s"),

to get filled timestamp when seeding?

播种时获取填充时间戳?

回答by sumit

Auto timestamp saving is only for Eloquent feature so you need to do manually like below for non eloquent feature

自动时间戳保存仅适用于 Eloquent 功能,因此您需要像下面这样手动操作非 eloquent 功能

DB::table('languages')->insert([
    'name'        => 'English',
    'flag'        => '',
    'abbr'        => 'en',
    'script'    => 'Latn',
    'native'    => 'English',
    'active'    => '1',
    'default'    => '1',
    'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
    'updated_at' => Carbon::now()->format('Y-m-d H:i:s')

]);

OR do it eloquently(Like you have seen in auto generated seeds like UserTableSeeder)

或者雄辩地做(就像你在像 UserTableSeeder 这样的自动生成的种子中看到的那样)

    $language = new Language();
    $language ->name = 'English';
    $language->flag'  = '',
    $language ->abbr  = 'en',
    $language->script ='Latn',
    $language->native ='English',
    $language->active ='1',
    $language->default ='1',
    $language->save();

Why to use carbon?Eloquent serves up Carbon for datetime and timestamp columns. By default it will serve up Carbon for the created_at, updated_at, and deleted_at columns. You can customize this in your models extending Eloquent\Model.

为什么要使用碳?Eloquent 为日期时间和时间戳列提供 Carbon。默认情况下,它会为 created_at、updated_at 和 deleted_at 列提供 Carbon。您可以在扩展 Eloquent\Model 的模型中自定义它。

Carbon\Carbon extends \DateTime, so there is no loss of functionality by using Carbon in favor of DateTime, only more benefit/flexibility.

Carbon\Carbon extends \DateTime,因此使用 Carbon 代替 DateTime 不会损失功能,只会带来更多好处/灵活性。

回答by MANDEEP

In users table seeder do this for timestamp. It works for me..

在用户表播种机中为时间戳执行此操作。这个对我有用..

    use Carbon\Carbon;



    $faker = Factory::create();
    $date = Carbon::now()->modify('-2 year');
    $createdDate = clone($date);
    DB::table('users')->insert([

        [

            'name' => "XYZ",
            'slug' => "xyz",
            'email' => "[email protected]",
            'password' => bcrypt('secret'),
            'bio' => $faker->text(rand(250, 300)),
            'created_at' => $createdDate,
            'updated_at' => $createdDate

        ],