Laravel 4数据库种子不起作用

Pat*_*iel 9 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
        )
    );
Run Code Online (Sandbox Code Playgroud)

接下来:php artisan db:seed.

php artisan db:seed --env=local
Database seeded!
Run Code Online (Sandbox Code Playgroud)

但:

mysql> select * from groups;
Empty set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)

Lau*_*nce 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
        ));    
    }
}
Run Code Online (Sandbox Code Playgroud)

现在运行php artisan db:seed- 它会工作.


Ste*_*ian 5

如果@The Shift Exchange返回一些错误,例如"找不到类用户",您可以尝试

DB::table('users')->insert(array(...)) 
Run Code Online (Sandbox Code Playgroud)

代替

User::create(array(...))
Run Code Online (Sandbox Code Playgroud)


Sin*_*dem 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

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

}
Run Code Online (Sandbox Code Playgroud)

续订自动加载器:

composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

种子数据库:

php artisan db:seed
Run Code Online (Sandbox Code Playgroud)

太好了,你做完了!