Laravel : Migrations & Seeding for production data

后端 未结 4 934
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 07:11

My application needs a pre registered data set to work. So i need to insert them in the database when i set up the application.

Laravel propose two mechanisms :

4条回答
  •  爱一瞬间的悲伤
    2020-12-02 07:30

    This is what I use in production.

    Since I run migration on each deployment

    artisan migrate
    

    I create a seeder (just to keep seeding data out of migration for easy access later) and then run that seeder along with the migration

    class YourTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {    
            //migrate your table // Example
            Schema::create('test_table', function(Blueprint $table)
            {
                $table->increments('id');
                $table->timestamps();
                $table->softDeletes();
            });
    
            //seed this table
            $seeder = new YourTableSeeder();
            $seeder->run();
        }
    
        /**
        * Reverse the migrations.
        *
        * @return void
        */
        public function down()
        {
            Schema::drop('test_table');
        }
    }
    

    I do not add this seed call to seeds/DatabaseSeeder.php to avoid running it twice on a new installation.

提交回复
热议问题