如何在 Laravel 5.3 中播种日期字段?

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

How to seed a date field in Laravel 5.3?

phplaravellaravel-5.3

提问by Octoxan

I've managed to set up a decent amount of seeding for data that needs to be in the database at launch. Everything was easy and working well until I needed to seed a DATE field with a default date.

我已经设法为启动时需要在数据库中的数据设置了大量的种子。在我需要为 DATE 字段设置默认日期之前,一切都很简单且运行良好。

I've tried the following...

我试过以下...

DatabaseSeeder.php

数据库浏览器.php

class SettingsTableSeeder extends Seeder {
    public function run()
    {
        Setting::create([
            'name' => 'Start Date',
            'date' => '2000-01-01'
        ]);
    }
}

In my model I've been told adding this shouldfix it, but it didn't.

在我的模型中,我被告知添加这个应该可以解决它,但它没有。

Setting.php

设置.php

protected $dates = [
    'created_at',
    'updated_at',
    'date'
];

Everytime I go to run the seeder it throws the error:

每次我去运行播种机时,它都会抛出错误:

[InvalidArgumentException]
The separation symbol could not be found
Unexpected data found.
Trailing data

If I remove the quotes around the date it changes to..

如果我删除日期周围的引号,它会更改为..

[InvalidArgumentException]
The separation symbol could not be found
Data missing

Any idea how one goes about seeding a default value for a DATE database field?

知道如何为 DATE 数据库字段设置默认值吗?

回答by Alexey Mezenin

If dateis in the $datesarray, insert Carbon instance instead of a string:

如果date$dates数组中,则插入 Carbon 实例而不是字符串:

Setting::create([
    'name' => 'Start Date',
    'date' => Carbon::parse('2000-01-01')
]);

You'll need to make sure that Carbonis available to use at the top of the file:

您需要确保Carbon可以在文件顶部使用:

use Carbon\Carbon;

It is auto-loaded by Laravel/Composer.

它由 Laravel/Composer 自动加载。

回答by Jaymin Panchal

Just try inserting the dateusing Carbonlike,

只需尝试插入dateusing 之Carbon类的,

class SettingsTableSeeder extends Seeder {
   public function run(){
      Setting::create([
        'name' => 'Start Date',
        'date' => \Carbon\Carbon::createFromDate(2000,01,01)->toDateTimeString()
      ]);
   }
}

回答by Octoxan

The given answers by Alexey Mezenin and Jaymin Panchal both still resulted in the trailing data error being thrown.

Alexey Mezenin 和 Jaymin Panchal 给出的答案仍然导致抛出尾随数据错误。

What ended up working for me was including...

最终对我有用的是包括......

use Carbon\Carbon;

at the top of the DatabaseSeeder.php file.

在 DatabaseSeeder.php 文件的顶部。

Then changing my Setting::create to...

然后将我的 Setting::create 更改为...

DB::table('settings')->insert([
    'name' => 'Start Date',
    'date' => Carbon::create('2000', '01', '01')
]);

for some reason I can't use Carbon with the standard Model::create method while seeding, but it works for DB::table('table')->insert.

出于某种原因,我不能在播种时将 Carbon 与标准 Model::create 方法一起使用,但它适用于 DB::table('table')->insert。