Laravel:迁移&生产数据的种子 [英] Laravel : Migrations & Seeding for production data

查看:361
本文介绍了Laravel:迁移&生产数据的种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要预先注册的数据集才能工作。所以我需要在设置应用程序时将它们插入数据库。

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 提出两种机制:

  • Database migrations : "They allow a team to modify the database schema and stay up to date on the current schema state."
  • Database seeding : "Laravel also includes a simple way to seed your database with test data using seed classes."

当我阅读本说明书时,这些解决方案似乎都不适用。

When I read this description, none of these solutions seems to be adapted.

一个类似的问题是 stackoverflow 回答。答案建议使用数据库种子器通过检测当前环境来填充数据库:

A similar question has been asked on stackoverflow and answered. The answer proposes to use the a database seeder to populate the database by detecting the current environment :

<?php

class DatabaseSeeder extends Seeder {

    public function run()
    {
            Eloquent::unguard();

            if (App::environment() === 'production')
            {
                $this->call('ProductionSeeder');
            }
            else
            {
                $this->call('StagingSeeder');
            }
    }

}

,这个解决方案工作。但我不知道这是正确的方法来做到这一点,因为通过插入数据使用种子,你失去了所有的优点提供的迁移机制(数据库upgrate,回滚...)

Of course, this solution works. But i am not sure that it is the right way to do this, because by inserting data using seeders you are losing all the advantages provided by the migration mechanism (database upgrate, rollback...)

我想知道这种情况下最好的做法是什么。

I want to know what is the best practice in this case.

推荐答案

Laravel开发是关于自由。所以,如果你需要种子你的生产数据库,并认为DatabaseSeeder是最好的地方,为什么不这样做?

Laravel development is about freedom. So, if you need to seed your production database and think DatabaseSeeder is the best place to do so, why not?

好,seeder主要是用于测试数据,但你会看到一些人使用它,因为你是。

Okay, seeder is mainly to be used with test data, but you'll see some folks using it as you are.

我看到这种重要的种子作为我的迁移的一部分,因为这是一个不能出来的我的数据库表和 artisan migrate 是每次部署我的应用程序的新版本,所以我只是做

I see this important kind of seed as part of my migration, since this is something that cannot be out of my database tables and artisan migrate is ran everytime I deploy a new version of my application, so I just do

php artisan migrate:make seed_models_table

in:

public function up()
{
    $models = array(
        array('name' => '...'),
    );

    DB::table('models')->insert($models);
}

这篇关于Laravel:迁移&amp;生产数据的种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆