insert([ [ 'person_id' => 1, 'joining_date' => '2014-9-22', 'leaving_date' => '2014-12-31', 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s') ], [ 'person_id' => 1, 'joining_date' => '2015-1-1', 'leaving_date' => null, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s') ], [ 'person_id' => 2, 'joining_date' => '2014-9-22', 'leaving_date' => null, 'created_at' => Carbon::now()->format('Y-m-d H:i:s'), 'updated_at' => Carbon::now()->format('Y-m-d H:i:s') ] ]);*/ // Iterate through existing People and randomly assign their ids // once or twice. foreach (App\Person::all() as $person) { // randomly decide if person has a state. $personHasState = rand(1,8) < 8; if($personHasState) { // if Person has a State, decide if it has one or two. $numberOfStates = rand(1,2); if($numberOfStates > 1) { $this->createPerson($person->id, $numberOfStates); } else { $this->createPerson($person->id, $numberOfStates); } } } } public function createPerson($id, $numberOfStates = 1) { // manually create faker Instance since you can't control iterations // in Factory-Files. $faker = Faker\Factory::create(); $startDate = "2014-04-01"; if($numberOfStates > 1) { // if member has 2 states, the first one must be a few years in the past. $lastLeaving = "2015-12-30"; $firstSecondJoiningDate = "2017-06-01"; $firstJoiningDate = $faker->dateTimeBetween($startDate, "2015-11-30"); $firstLeavingDate = $faker->dateTimeBetween($firstJoiningDate, $lastLeaving); $secondJoiningDate = $faker->dateTimeBetween($firstSecondJoiningDate, 'now'); $secondLeavingDate = NULL; if(rand(1,8) === 8) { $secondLeavingDate = $faker->dateTimeBetween($secondJoiningDate, 'now'); } factory(App\MemberState::class, 1)->create([ 'person_id' => $id, "joining_date" => $firstJoiningDate, "leaving_date" => $firstLeavingDate, ]); factory(App\MemberState::class, 1)->create([ 'person_id' => $id, "joining_date" => $secondJoiningDate, "leaving_date" => $secondLeavingDate, ]); } else { $joiningDate = $faker->dateTimeBetween($startDate, 'now - 30 days'); $leavingDate = NULL; if(rand(1,5) === 5) { $leavingDate = $faker->dateTimeBetween($joiningDate, 'now'); } factory(App\MemberState::class, 1)->create([ 'person_id' => $id, "joining_date" => $joiningDate, "leaving_date" => $leavingDate, ]); } } }