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 - 测试数据与样本数据
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
