user2075215
user2075215

Reputation: 379

laravel artisan seed Object of class DateTime could not be converted to string

I get an error when running (in laravel)

php artisan migrate --seed

I get

Object of class DateTime could not be converted to string

My Users seed table looks like this

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();

        $users = [
            [
                'firstname' => 'David',
                'surname' => 'Smith',
                'email' => '[email protected]',
                'created_at' => new DateTime,
                'updated_at' => new DateTime
            ],
            [
                'firstname' => 'Mike',
                'surname' => 'Smith',
                'email' => '[email protected]',
                'created_at' => new DateTime,
                'updated_at' => new DateTime
            ]
        ];

        DB::table('users')->insert($users);
    }

}

Any ideas why artisian/php is complaining?

Upvotes: 2

Views: 9623

Answers (4)

Rick
Rick

Reputation: 13490

The new DateTime() function works perfectly with laravel make sure that the proper value is set for the table name in app/models/tablename.php

protected $table = 'TableName';

Another note, the class is always singular and the tables tend to be plural for example PHP class

user.php 

Table Name

Users

Lastly make sure that the key in the array is correct

'updated_at' vs 'updated_at '<--Note the empty space

Upvotes: 0

user2983930
user2983930

Reputation: 11

"New DateTime" does work without a problem in Laravel, apparently something has been added to Laravel that lets it overcome this error.

What I've seen happen, though, is that there are other errors in the Seed data, such as specifying a field that doesn't exist, or specifying two seeds that have different columns, and the "DateTime" error appears by default. Try temporarily removing your created_at and updated_at fields from the seed and Laravel will reveal what the actual problem is.

Upvotes: 1

marcanuy
marcanuy

Reputation: 23972

You can use the same package that Laravel uses https://github.com/briannesbitt/Carbon for managing dates, to fix that, fill datetime columns with appropriate values:

class UsersTableSeeder extends Seeder
{
    public function run()
    {
        DB::table('users')->delete();
        $dt = Carbon::now();
        $dateNow = $dt->toDateTimeString();
        $users = [
            [
                'firstname' => 'David',
                'surname' => 'Smith',
                'email' => '[email protected]',
                'created_at' => $dateNow,
                'updated_at' => $dateNow
            ],
            [
                'firstname' => 'Mike',
                'surname' => 'Smith',
                'email' => '[email protected]',
                'created_at' => $dateNow,
                'updated_at' => $dateNow
            ]
        ];

        DB::table('users')->insert($users);
    }

}

Upvotes: 2

Alen
Alen

Reputation: 1788

DateTime return time and you need string, so to get time in string format try something like this:

$newDate = DateTime::createFromFormat("l dS F Y", $someDate);
$newDate = $newDate->format('d/m/Y'); 

Change formating as you need it.

Upvotes: 1

Related Questions