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

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

问题描述

我可能误解了它究竟是如何工作的,但最好的方法是什么?我有一些想法,但它看起来很老套.

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 Seeds - 测试数据与样本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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