美好的一天,
当我试图为我的数据库播种时,我遇到了错误"类DateTime的对象无法转换为字符串".
这是我的迁移代码:
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();
});
}
Run Code Online (Sandbox Code Playgroud)
和我的播种者:
<?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);
}
}
Run Code Online (Sandbox Code Playgroud)
我放的时候收到错误'created_at'=> new DateTime
.我怎样才能解决这个问题?谢谢!
Ant*_*iro 18
尝试使用Carbon创建日期(Laravel在内部使用它):
'expirationDate' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
Run Code Online (Sandbox Code Playgroud)
要么
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
Run Code Online (Sandbox Code Playgroud)
小智 7
如果你想将种子随机化为模拟数据,我建议使用PHP Faker.否则你可以使用
date('Y-m-d H:i:s');
Run Code Online (Sandbox Code Playgroud)
使用Faker
https://github.com/fzaninotto/Faker
添加到composer.json
"fzaninotto/faker" : "dev-master",
Run Code Online (Sandbox Code Playgroud)
包括命名空间
use Faker\Factory as Faker;
Run Code Online (Sandbox Code Playgroud)
初始化Faker
$faker = Faker::create();
Run Code Online (Sandbox Code Playgroud)
开始伪造东西
$faker->dateTime();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9015 次 |
最近记录: |