Laravel DB种子-测试数据v样本数据 [英] Laravel DB Seeds - Test Data v Sample Data

查看:68
本文介绍了Laravel DB种子-测试数据v样本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能误解了它是如何工作的,但是实现这一目标的最佳方法是什么?我有一些想法,但似乎很不客气.

I'm probably misunderstanding exactly how this works, but what's the best way to accomplish this? I have something in mind but it seems quite hacky.

我有一组用于测试应用程序的示例数据.这是通过Laravel内置的播种机播种的.其中包含示例用户,地址,文档等.

I have a set of sample data which I use to test my application. This is seeded via the built in seeder in Laravel. This contains things like example users, addresses, documents etc.

我还有一组默认数据,应该在生产中使用.我目前将其直接添加到迁移中.例如,如果要添加一个account_roles表,则可能在迁移的底部添加以下内容

I also have a set of default data which should go in production. I currently add this directly in the migration. For example, if I was adding a table for account_roles, I might include the following at the bottom of the migration

$account_admin = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_ADMIN');
$account_owner = array('role' => 'Account Administrator', 'flag' => 'ACCOUNT_OWNER');
DB::table('account_roles')->insert($account_admin);
DB::table('account_roles')->insert($account_owner);

这样,在生产环境中,我只需迁移数据库以插入任何生产就绪的数据库值,而在暂存/开发过程中,我可以刷新迁移,然后使用示例数据为数据库播种.

This way, on production, I just migrate the database to insert any production ready database values, and on staging/development, I can refresh the migrations and then seed the database with sample data.

还有其他(更好)的方法吗?

Is there any other (better) way to do this?

推荐答案

您可以在种子文件中对当前环境进行检查,并根据需要进行种子

You could run a check on the current environment in your seeder file, and seed as needed

<?php

class DatabaseSeeder extends Seeder {

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

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

}

这篇关于Laravel DB种子-测试数据v样本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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