用于硬编码的桌子的Laravel播种机

时间:2016-07-05 20:30:21

标签: php mysql laravel laravel-seeding

我的数据库中有几个表,仅供我的应用程序参考。

例如,一个表有两列,State和State Abbreviation,并且所有50个状态及其缩写为行。

State       | State Abbreviation
Alabama     | AL
Alaska      | AK
...

我想在Laravel中为这些表编写播种器,因为当我启动应用程序的新版本时,我希望这些表存在。

存储此数据和创建这些播种机的最佳方法是什么?

2 个答案:

答案 0 :(得分:3)

播种机不仅仅是测试数据 - 它们非常适合静态数据。

仅仅手动创建播种机不会花太长时间,但您可以使用 ggoforth https://github.com/ggoforth/laravel-5-us-states-seeder/blob/master/StatesSeeder.php

的礼貌来代替重建轮子
<?php

use App\State;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class StatesSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {

        DB::table('states')->truncate();

        State::create(['name' => 'Alaska', 'code' => 'AK']);
        State::create(['name' => 'Alabama', 'code' => 'AL']);
        State::create(['name' => 'American Samoa', 'code' => 'AS']);
        State::create(['name' => 'Arizona', 'code' => 'AZ']);
        State::create(['name' => 'Arkansas', 'code' => 'AR']);
        State::create(['name' => 'California', 'code' => 'CA']);
        State::create(['name' => 'Colorado', 'code' => 'CO']);
        State::create(['name' => 'Connecticut', 'code' => 'CT']);
        State::create(['name' => 'Delaware', 'code' => 'DE']);
        State::create(['name' => 'District of Columbia', 'code' => 'DC']);
        State::create(['name' => 'Federated States of Micronesia', 'code' => 'FM']);
        State::create(['name' => 'Florida', 'code' => 'FL']);
        State::create(['name' => 'Georgia', 'code' => 'GA']);
        State::create(['name' => 'Guam', 'code' => 'GU']);
        State::create(['name' => 'Hawaii', 'code' => 'HI']);
        State::create(['name' => 'Idaho', 'code' => 'ID']);
        State::create(['name' => 'Illinois', 'code' => 'IL']);
        State::create(['name' => 'Indiana', 'code' => 'IN']);
        State::create(['name' => 'Iowa', 'code' => 'IA']);
        State::create(['name' => 'Kansas', 'code' => 'KS']);
        State::create(['name' => 'Kentucky', 'code' => 'KY']);
        State::create(['name' => 'Louisiana', 'code' => 'LA']);
        State::create(['name' => 'Maine', 'code' => 'ME']);
        State::create(['name' => 'Marshall Islands', 'code' => 'MH']);
        State::create(['name' => 'Maryland', 'code' => 'MD']);
        State::create(['name' => 'Massachusetts', 'code' => 'MA']);
        State::create(['name' => 'Michigan', 'code' => 'MI']);
        State::create(['name' => 'Minnesota', 'code' => 'MN']);
        State::create(['name' => 'Mississippi', 'code' => 'MS']);
        State::create(['name' => 'Missouri', 'code' => 'MO']);
        State::create(['name' => 'Montana', 'code' => 'MT']);
        State::create(['name' => 'Nebraska', 'code' => 'NE']);
        State::create(['name' => 'Nevada', 'code' => 'NV']);
        State::create(['name' => 'New Hampshire', 'code' => 'NH']);
        State::create(['name' => 'New Jersey', 'code' => 'NJ']);
        State::create(['name' => 'New Mexico', 'code' => 'NM']);
        State::create(['name' => 'New York', 'code' => 'NY']);
        State::create(['name' => 'North Carolina', 'code' => 'NC']);
        State::create(['name' => 'North Dakota', 'code' => 'ND']);
        State::create(['name' => 'Northern Mariana Islands', 'code' => 'MP']);
        State::create(['name' => 'Ohio', 'code' => 'OH']);
        State::create(['name' => 'Oklahoma', 'code' => 'OK']);
        State::create(['name' => 'Oregon', 'code' => 'OR']);
        State::create(['name' => 'Palau', 'code' => 'PW']);
        State::create(['name' => 'Pennsylvania', 'code' => 'PA']);
        State::create(['name' => 'Puerto Rico', 'code' => 'PR']);
        State::create(['name' => 'Rhode Island', 'code' => 'RI']);
        State::create(['name' => 'South Carolina', 'code' => 'SC']);
        State::create(['name' => 'South Dakota', 'code' => 'SD']);
        State::create(['name' => 'Tennessee', 'code' => 'TN']);
        State::create(['name' => 'Texas', 'code' => 'TX']);
        State::create(['name' => 'Utah', 'code' => 'UT']);
        State::create(['name' => 'Vermont', 'code' => 'VT']);
        State::create(['name' => 'Virgin Islands', 'code' => 'VI']);
        State::create(['name' => 'Virginia', 'code' => 'VA']);
        State::create(['name' => 'Washington', 'code' => 'WA']);
        State::create(['name' => 'West Virginia', 'code' => 'WV']);
        State::create(['name' => 'Wisconsin', 'code' => 'WI']);
        State::create(['name' => 'Wyoming', 'code' => 'WY']);
        State::create(['name' => 'Armed Forces Africa', 'code' => 'AE']);
        State::create(['name' => 'Armed Forces Americas (except Canada)', 'code' => 'AA']);
        State::create(['name' => 'Armed Forces Canada', 'code' => 'AE']);
        State::create(['name' => 'Armed Forces Europe', 'code' => 'AE']);
        State::create(['name' => 'Armed Forces Middle East', 'code' => 'AE']);
        State::create(['name' => 'Armed Forces Pacific', 'code' => 'AP']);
    }

}

答案 1 :(得分:2)

我会使用raw sql:

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
     public function seed($table)
    {
        $this->command->info('Seeding '.$table);
        $this->call($table.'TableSeeder');
    }

    public function run()
    {
        DB::unprepared(file_get_contents(database_path('states.sql')));
    }
}

然后在sql文件中:

INSERT INTO `states` (`id`, `state`, `abbrev`)
VALUES
    (1,'Alabama', 'AL'),
    (2,'Alaska', 'AK)...;