Laravel 4数据库种子不起作用

时间:2013-02-09 20:47:27

标签: php laravel eloquent laravel-4

我遵循本教程: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

本教程:http://laravelbook.com/laravel-database-seeding/

但是,当我尝试运行php artisan db:seed时,没有任何反应。

我试试这个:

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

接下来:php artisan db:seed

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

可是:

mysql> select * from groups;
Empty set (0.00 sec)

3 个答案:

答案 0 :(得分:31)

本教程中的示例是错误的 - 因为种子在Beta 1和Beta 2之间的工作方式发生了变化。

将您的DatabaseSeeder.php文件更改为此文件 - 它适用于本教程:

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

现在运行php artisan db:seed - 它会起作用。

答案 1 :(得分:5)

如果@ Shift Exchange返回一些错误,例如“未找到类用户”,您可以尝试

DB::table('users')->insert(array(...)) 

而不是

User::create(array(...))

答案 2 :(得分:5)

创建播种机:

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

}

将其添加到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!');
        ...
    }

}

续订自动加载器:

composer dump-autoload

为数据库设定种子:

php artisan db:seed

很好,你已经完成了!