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)
4

3 回答 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- 它会工作。

于 2013-02-10T07:13:35.330 回答
6

创建你的播种机:

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

太好了,你完成了!

于 2013-12-11T16:18:50.547 回答
5

如果@The Shift Exchange 返回诸如“找不到类用户”之类的错误,您可以尝试

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

代替

User::create(array(...))
于 2013-07-28T23:07:48.970 回答