Laravel DB Seeds - Test Data v Sample Data(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 - 测试数据与样本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel DB Seeds - 测试数据与样本数据
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01