【问题标题】:Laravel 4 database seed doesn't workLaravel 4 数据库种子不起作用
【发布时间】:2013-01-25 07:57:35
【问题描述】:

我遵循这个教程: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)

【问题讨论】:

    标签: php laravel eloquent laravel-4


    【解决方案1】:

    本教程中的示例是错误的 - 因为种子在 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 - 它会工作。

    【讨论】:

    • 我会测试并给您反馈,请稍等。
    • DB::table('users')-&gt;delete(); 这行不受欢迎
    【解决方案2】:

    如果@The Shift Exchange 返回诸如 "Class User not found" 之类的错误,您可以尝试

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

    而不是

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

    【讨论】:

      【解决方案3】:

      创建您的播种机:

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

      太好了,大功告成!

      【讨论】:

      • 有没有办法像迁移文件一样在数据库种子文件之外填充它?我已经测试了几种方法,但没有一个有效。它仅在我在 databaseseeder.php 文件中填充数据时才起作用
      猜你喜欢
      • 1970-01-01
      • 2014-01-26
      • 2016-01-20
      • 2015-02-04
      • 2018-01-30
      • 2014-06-13
      • 1970-01-01
      • 2013-10-26
      • 2015-09-04
      相关资源
      最近更新 更多