85 lines
2.7 KiB
PHP
85 lines
2.7 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use Carbon\Carbon;
|
|
|
|
class TestMemberStatesTableSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
// 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_bool(88);
|
|
|
|
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_bool(12)) {
|
|
$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_bool(20)) {
|
|
$leavingDate = $faker->dateTimeBetween($joiningDate, 'now');
|
|
}
|
|
|
|
factory(App\MemberState::class, 1)->create([
|
|
'person_id' => $id,
|
|
"joining_date" => $joiningDate,
|
|
"leaving_date" => $leavingDate,
|
|
]);
|
|
}
|
|
}
|
|
}
|