Laravel 4 数据库种子不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14791421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:32:56  来源:igfitidea点击:

Laravel 4 database seed doesn't work

phplaraveleloquentlaravel-4

提问by Patrick Maciel

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

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

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

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

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

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

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

But:

但:

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

回答by Laurence

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

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

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

将您的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
        ));    
    }
}

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

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

回答by Stelian

If @The Shift Exchange return some error like "Class User not found"you could try

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

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

instead of

代替

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

回答by Sinan Eldem

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

// 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!

太好了,你完成了!