Custom Faker Provider: Reuse city

766 Views Asked by At

I built a custom Faker Provider to generate team names.

This is how that looks like:

<?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);
    }
}

This works fine in my Factory:

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

But: The TeamProvider generates a city and this is not the same city the Factory's faker is generating.

So the teamName is (just an example) AC Glasgow 1990 and the city of AC Glasgow is London ;-) This does not make a lot of sense ;-)

So my question is: How can I reuse the city in this case to get valid results?

1

There are 1 best solutions below

0
On

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.