Ad

Factory Same ID For A Column, Auto-incremental, Laravel

- 1 answer

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();
    }
Ad

Answer

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;
    }
}
Ad
source: stackoverflow.com
Ad