jeudi 7 juin 2018

How to increment one field of column while creating factory in laravel 5.6?

I want to generate 1000 users and then generate another 1000 record for the NewUser table which is working properly but the registration_no field is breaking the unique key constraint.

So i guess its always storing value 1 in registration field.

        $i = 1;

        factory(App\User::class, 1000)->states('newuser')->create()
        ->each(function ($u) use($i)
        {

            $u->newuser()->save(factory(App\NewUser::class)->make([
                'registration_no' => $i,
            ]));

            $i++;

        })

So how do I actually increment the field by 1 everytime new user is created ?

Aucun commentaire:

Enregistrer un commentaire