laravel 模型工厂_使用模型工厂生成虚拟Laravel数据

laravel 模型工厂

A while back, if I needed dummy data for my local application environment — it involved importing a database partial of an existing site, or modifying MySQL's sakila database, or just some funky way to get data. Laravel gave us seeding (not a new concept). Seeding lets the developer input data into the local database like it would appear on the main site.

不久前,如果我需要本地应用程序环境的虚拟数据-它涉及到导入现有站点的数据库部分,或修改MySQL的 sakila数据库,或者只是一些时髦的方式来获取数据。 Laravel给了我们种子(不是一个新概念)。 播种使开发人员可以像在主站点上一样将数据输入本地数据库。

Take this scenario for example. You need to create a database with a thousand users, each user has a certain number of posts (0 - n) attached to them. You would not do this:

以这种情况为例。 您需要创建一个包含一千个用户的数据库,每个用户都具有一定数量的帖子(0-n)。 您不会这样做:

// manually make a list of users
$users = [
    [
        'username' => 'firstuser',
        'name' => 'first user',
        'email' => 'firstuser@email.com'
    ],
    [
        'username' => 'seconduser',
        'name' => 'second user',
        'email' => 'seconduser@email.com'
    ]
    ...
];

This is silly, nobody wants to do this a thousand time. Thanks to libraries like fzaninotto/faker, we can create a lot of fake data that makes sense.

这很愚蠢,没人愿意这样做一千次。 fzaninotto/faker类的库,我们可以创建很多有意义的虚假数据。

The fzaninotto/faker library is available on packagist so we can install it via composer. You do not need to install this package if you are using laravel 5 or higher.

fzaninotto/faker库在packagist上可用,因此我们可以通过composer安装它。 如果您使用的是laravel 5或更高版本,则无需安装此软件包。

composerrequire fzaninotto/faker --dev

After installing faker, we can then use our seeder class to create as many users as we want. In the run method of the UserTableSeeder class or whatever you named your seed class, do this.

在安装了faker ,我们可以使用种子类来创建任意数量的用户。 在UserTableSeeder类的run方法或您命名为种子类的任何方法中,执行此操作。

public function run() {
     
    $faker = Faker\Factory::create();

    for($i = 0; $i < 1000; $i++) {
     
        App\User::create([
            'username' => $faker->userName,
            'name' => $faker->name,
            'email' => $faker->email
        ]);
    }
}

With the above snippet, we can create a thousand users without having to go through a lot of manual labor.

使用上面的代码片段,我们可以创建一千个用户,而无需进行大量的体力劳动。

Although this method saves us a lot of time and effort, it is not the most effective solution out there. When you want to create fake data for testing, you then have to copy this code and paste it in your test class. This method is not maintanable because you now have duplicate, changing a table schema would be a nightmare.

尽管此方法为我们节省了大量时间和精力,但它并不是目前最有效的解决方案。 当您想创建用于测试的假数据时,则必须复制此代码并将其粘贴到测试类中。 此方法不可维护,因为您现在已有重复项,更改表架构将是一场噩梦。

Don't forget, you should always keep your code DRY.

别忘了,您应该始终保持代

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值