如何在 Laravel 4.1 上播种时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22738729/
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
How to seed timestamps on laravel 4.1?
提问by melvnberd
Good day,
再会,
I was having an error "Object of class DateTime could not be converted to string" when Im trying to seed my database.
当我尝试为我的数据库设置种子时,我遇到错误“无法将类 DateTime 的对象转换为字符串”。
here is my migration code:
这是我的迁移代码:
public function up()
{
Schema::create('tblinventory', function(Blueprint $table) {
$table->increments('id');
$table->integer('itemId');
$table->enum('status', array('active','inactive'))->default(null)->nullable();
$table->float('purchasePrice');
$table->float('sellingPrice');
$table->date('expirationDate');
$table->float('ReceivedQuantity');
$table->float('soldQuantity');
$table->timestamps();
});
}
and my seeder:
和我的播种机:
<?php
class InventoryTableSeeder extends Seeder {
public function run()
{
// Uncomment the below to wipe the table clean before populating
DB::table('tblinventory')->truncate();
$insert = [
[
'itemId' => '1',
'status' => 'inactive',
'ReceivedQuantity'=>'100',
'SoldQuantity'=>'93',
'sellingPrice'=>'4.5',
'purchasePrice'=>'3.5',
'created_at' => new DateTime,
'expirationDate'=>date('2015-02-22')
],
[
'itemId' => '1',
'status' => 'inactive',
'ReceivedQuantity'=>'300',
'SoldQuantity'=>'300',
'sellingPrice'=>'4.75',
'purchasePrice'=>'3.65',
'expirationDate'=>date('2015-02-22')
],
[
'itemId' => '2',
'status' => 'inactive',
'ReceivedQuantity'=>'100',
'SoldQuantity'=>'93',
'sellingPrice'=>'3.5',
'purchasePrice'=>'2.5',
'expirationDate'=>date('2014-07-22')
],
[
'itemId' => '3',
'status' => 'inactive',
'ReceivedQuantity'=>'100',
'SoldQuantity'=>'93',
'sellingPrice'=>'12.5',
'purchasePrice'=>'10.5',
'expirationDate'=>date('2017-01-02')
],
[
'itemId' => '3',
'status' => 'inactive',
'ReceivedQuantity'=>'100',
'SoldQuantity'=>'100',
'sellingPrice'=>'14.5',
'purchasePrice'=>'13.5',
'expirationDate'=>date('2017-07-22')
],
[
'itemId' => '4',
'status' => 'inactive',
'ReceivedQuantity'=>'100',
'SoldQuantity'=>'93',
'sellingPrice'=>'24.5',
'purchasePrice'=>'23.5',
'expirationDate'=>date('2015-07-22')
]
];
DB::table('tblinventory')->insert($insert);
// Uncomment the below to run the seeder
// DB::table('inventories')->insert($inventories);
}
}
I get the error when I put 'created_at'=> new DateTime
. How can I fix this? thank you!
我把'created_at'=> new DateTime
. 我怎样才能解决这个问题?谢谢你!
回答by Antonio Carlos Ribeiro
Try to create your dates using Carbon (Laravel uses it internally):
尝试使用 Carbon 创建日期(Laravel 在内部使用它):
'expirationDate' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
or
或者
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
回答by user3477029
I would recommend using PHP Faker if you want to randomize your seeds for mock data. Otherwise you can just use
如果您想随机化模拟数据的种子,我建议使用 PHP Faker。否则你可以使用
date('Y-m-d H:i:s');
Using Faker
使用 Faker
https://github.com/fzaninotto/Faker
https://github.com/fzaninotto/Faker
Add to composer.json
添加到 composer.json
"fzaninotto/faker" : "dev-master",
Include the Namespace
包括命名空间
use Faker\Factory as Faker;
Initialize Faker
初始化 Faker
$faker = Faker::create();
Start Faking Stuff
开始伪造东西
$faker->dateTime();
回答by dasper
I am a little late to the party here but I wanted to give another option that others may find useful.
我参加聚会有点晚了,但我想提供其他人可能觉得有用的另一种选择。
If you have already created your models using Eloquent, then there is another option to have Eloquent fill those fields for you automatically by using the orm. Assuming your btlinventory has a model name of Inventory:
如果您已经使用 Eloquent 创建了模型,那么还有一个选项可以让 Eloquent 使用 orm 自动为您填充这些字段。假设您的 btlinventory 的模型名称为 Inventory:
foreach($insert as $row ){
$inventory = new Inventory;
$inventory->fill($row);
$inventory->save();
}
insert is a query builder method so by itself it will not handle any Eloquent tasks, however, you can always chain query builder methods off of an Eloquent object and then it would work. If you use Inventory::create($array);
and still have issues then I hear this may get fixed by explicitly stating public $timestamps = true;
in your model.
insert 是一个查询构建器方法,因此它本身不会处理任何 Eloquent 任务,但是,您始终可以将查询构建器方法从 Eloquent 对象链接起来,然后它就可以工作了。如果您使用Inventory::create($array);
并且仍然有问题,那么我听说这可能会通过$timestamps = true;
在您的模型中明确声明 public来解决。