Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed timestamps on laravel 4.1?

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!

like image 904
melvnberd Avatar asked Mar 30 '14 00:03

melvnberd


People also ask

How do you make a seeder?

To create seeders, you may use the make:seeder Artisan command. All seeders generated will be placed in the database/seeds directory. Generated seeders will contain one method: run . You may insert data into your database in this method.

What is database seeding in Laravel?

Laravel includes the ability to seed your database with data using seed classes. All seed classes are stored in the database/seeders directory. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.


2 Answers

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()
like image 118
Antonio Carlos Ribeiro Avatar answered Oct 23 '22 15:10

Antonio Carlos Ribeiro


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();
like image 42
user3477029 Avatar answered Oct 23 '22 16:10

user3477029