0

我构建了一个自定义 Faker Provider 来生成团队名称。

这是这样的:

<?php

namespace App\Faker;

use Carbon\Carbon;
use Faker\Provider\Base;

class TeamProvider extends Base
{
    ...

    public function teamName()
    {
       
        $name[] = static::randomElement(static::$prefixes);
        ...
        $name[] = $this->generator->city;
        //some more stuff to create a nice team name

        return implode(" ", $name);
    }
}

这在我的工厂中运行良好:

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'name' => $this->faker->teamName,
            'city' => $this->faker->city
        ];
    }

但是:TeamProvider生成一个城市,这不是工厂的伪造者正在生成的同一个城市。

所以teamName是(只是一个例子)AC Glasgow 1990和城市AC GlasgowLondon;-)这没有多大意义;-)

所以我的问题是:在这种情况下如何重用city以获得有效结果?

4

1 回答 1

0

As I read through the docs again I mentioned my problem. Faker is not used to create data that are somehow "logically" related just like real person's data. It just fills model's fields with valid data. So it's useful for tinkering and testing, but not for bootstrapping some valid and consistent data.

于 2021-01-28T19:44:38.037 回答