samedi 16 juin 2018

Laravel Seeding not working

Consider the below code:

foreach ($departments as $department)
        {
            factory(App\User::class, 100)->create()
            ->each(function ($u) use($department)
            {

                $u->employee()->save(factory(App\Employee::class)->make([
                    'department' => $department,
                    'emp_id' => '',
                ]));

            });

        }

Now for emp_id I want to have incremental value from 1 to 100 for each iteration of the outer foreach loop.

Example:

1st iteration 100 users and 100 employees record should be created with emp_id 1 to 100

2nd iteration again 100 users and 100 employees record should be created with emp_id 1 to 100 and so on.

I tried to declare $i=1 before the factory and then increment $i withing each() but it did not work and gave duplicate key error.

Note that the unique constraint in my table for employee is department-emp_id



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire