Laravel 4数据库种子无法工作。

10

我按照这个教程进行操作: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个回答

33

教程中的示例是错误的——因为 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 - 它将起作用。


我会测试并给你反馈,稍等一下。 - Patrick Maciel
1
该行代码 DB::table('users')->delete(); 是不可取的。 - Kevin

6

创建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!


有没有一种方法可以在迁移文件之外的地方填充数据,比如数据库填充器文件?我尝试了几种方法,但都不起作用。只有在databaseseeder.php文件中填充数据时才有效。 - lonerunner

5
如果 @The Shift Exchange 返回一些错误,比如“找不到用户类”,您可以尝试:
DB::table('users')->insert(array(...)) 

代替
User::create(array(...))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接