Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 database seed doesn't work

I follow this tutorial: http://fideloper.com/post/41750468389/laravel-4-uber-quick-start-with-auth-guide?utm_source=nettuts&utm_medium=article&utm_content=api&utm_campaign=guest_author

And this tutorial: http://laravelbook.com/laravel-database-seeding/

But, when I try run php artisan db:seed, nothing happens.

I try this:

<?php
    // app/database/seeds/groups.php
    return array(
        'table' => 'groups',
        array(
            'name' => 'Administrador',
            'description' => '<p>Permissão total no sistema</p>',
            'created_at' => new DateTime,   
            'updated_at' => new DateTime
        ),
        array(
            'name' => 'Moderadores',
            'description' => '<p>Podem apenas postar e moderar comentários</p>',
            'created_at' => new DateTime,   
            'updated_at' => new DateTime
        )
    );

And next: php artisan db:seed.

php artisan db:seed --env=local
Database seeded!

But:

mysql> select * from groups;
Empty set (0.00 sec)
like image 578
Patrick Maciel Avatar asked Feb 09 '13 20:02

Patrick Maciel


2 Answers

The example in the tutorial is wrong - because there was a change to the way seeds work between Beta 1 and Beta 2.

Change your DatabaseSeeder.php file to this - and it will work for the tutorial:

<?php

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call('UserTableSeeder');
    }

}

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();
        User::create(array(
                'id' => 1,
                'username' => 'firstuser',
                'password' => Hash::make('first_password'),
                'created_at' => new DateTime,
                'updated_at' => new DateTime
        ));
        User::create(array(
                'id' => 2,
                'username' => 'seconduser',
                'password' => Hash::make('second_password'),
                'created_at' => new DateTime,
                'updated_at' => new DateTime
        ));    
    }
}

Now run php artisan db:seed - and it will work.

like image 120
Laurence Avatar answered Sep 20 '22 03:09

Laurence


Create your seeder:

<?php

// NoticesTableSeeder.php    
class NoticesTableSeeder extends Seeder {

    public function run()
    {
        DB::table('notices')->truncate();

        $notices = [
            ['title' => 'Our Web Page Is Back Online!', 'body' => 'Our web site is online again. Thanks for visiting.', 'created_at' => new DateTime],
            ['title' => 'Sample New Notice 1', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
            ['title' => 'Sample New Notice 2', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
            ['title' => 'Sample New Notice 3', 'body' => 'Sample new notice content.', 'created_at' => new DateTime],
            ['title' => 'Sample New Notice 4', 'body' => 'Sample new notice content.', 'created_at' => new DateTime]
        ];
        DB::table('notices')->insert($notices);
    }

}

Add it to the DatabaseSeeder.php

// DatabaseSeeder.php
class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        ...
        $this->call('NoticesTableSeeder');
        $this->command->info('Notices table seeded!');
        ...
    }

}

Renew your autoloader:

composer dump-autoload

Seed the database:

php artisan db:seed

Great, you are done!

like image 36
Sinan Eldem Avatar answered Sep 20 '22 03:09

Sinan Eldem