Reputation: 3163
Good day,
I was having an error "Object of class DateTime could not be converted to string" when Im trying to seed my database.
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!
Upvotes: 7
Views: 9992
Reputation: 722
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:
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.
Upvotes: 0
Reputation: 87719
Try to create your dates using Carbon (Laravel uses it internally):
'expirationDate' => \Carbon\Carbon::createFromDate(2014,07,22)->toDateTimeString()
or
'created_at' => \Carbon\Carbon::now()->toDateTimeString()
Upvotes: 20
Reputation: 81
I would recommend using PHP Faker if you want to randomize your seeds for mock data. Otherwise you can just use
date('Y-m-d H:i:s');
Using Faker
https://github.com/fzaninotto/Faker
Add to composer.json
"fzaninotto/faker" : "dev-master",
Include the Namespace
use Faker\Factory as Faker;
Initialize Faker
$faker = Faker::create();
Start Faking Stuff
$faker->dateTime();
Upvotes: 8