laravellaravel-6fakerlaravel-factory

Factory same ID for a column, auto-incremental, Laravel


I try to give to a column the ID of that row, witch is autoincremental. Right now are random values .

//From Factory

    return [
       //code...
       'register_id' => $faker->unique()->numberBetween($min = 1, $max = 100),
       //code...
   ];  

    //From Seeder
    public function run()
    {
        factory(App\Person::class, 100)->create();
    }

Solution

  • I found an answer, here , and I edited for my case.

    $autoIncrement = autoIncrement();
    
    $factory->define(Person::class, function (Faker $faker) use ($autoIncrement) {
        $autoIncrement->next();
    
        //code
    
        return [
           'register_id' => $autoIncrement->current(),
        ]
    
    });
    
    function autoIncrement()
    {
        for ($i = 0; $i < 1000; $i++) {
            yield $i;
        }
    }