2014-03-30 31 views
5

美好的一天,如何在laravel 4.1上播種時間戳?

當我嘗試播種我的數據庫時,出現「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(); 
    }); 
} 

和我播種機:

<?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); 
} 

} 

我得到的錯誤,當我把'created_at'=> new DateTime。我怎樣才能解決這個問題?謝謝!

回答

18

嘗試創建使用碳日期(Laravel內部使用它):

'expirationDate' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString() 

'created_at' => \Carbon\Carbon::now()->toDateTimeString() 
+0

如果我刪除''created_at'=> new DateTime',那麼感謝您提示@antonio先生的工作。那麼,我怎麼才能種子created_at領域?謝謝 – melvnberd

+0

只需使用Carbon :: now(),編輯後即可顯示如何。 –

+1

它的工作!謝謝你的時間:) – melvnberd

8

,如果你想隨機的種子模擬數據我會建議使用PHP法克爾。否則,你可以只使用

date('Y-m-d H:i:s'); 

使用法克爾

https://github.com/fzaninotto/Faker

加入composer.json

"fzaninotto/faker" : "dev-master", 

包括命名空間

use Faker\Factory as Faker; 

初始化法克爾

$faker = Faker::create(); 

開始僞造的東西

$faker->dateTime(); 
+0

謝謝你的提示!我試着用它以後:)這是非常有幫助的。 – melvnberd

0

我有點遲到了這裏,但我想給另一種選擇,其他人可能會發現有用的。

如果您已經使用Eloquent創建了模型,則還有另一個選項可以使用orm自動填充這些字段。假設你的btlinventory有庫存的型號名稱:

foreach($insert as $row){ 
     $inventory = new Inventory; 
     $inventory->fill($row); 
     $inventory->save(); 
    } 

刀片是查詢生成器方法,所以它本身不會處理任何雄辯的任務,但是,您可以隨時鏈查詢生成器方法關閉雄辯對象,然後它會工作。如果您使用Inventory::create($array);並仍然存在問題,那麼我聽說這可能會通過在您的模型中明確指出公開$timestamps = true;得到解決。