Hi Forum
I am trying to implement a seeder class that fills my table vacations with a couple of rows of vacations.
The Script is supposed to generate a random start date between to dates and then adds a random number of weeks to the start date to generate an end date.
I have tried the following code inside a view file, and it works.
However when running it in a DatabaseSeeder class it does set the exact same end date as the start date. So the newly calculated end date is not inserted or not interpreted by the script.
Here is the code
class VacationsTableSeeder extends Seeder {
public function run()
{
$faker = Faker::create();
$members = User::whereHas(
'roles', function ($q) {
$q->where( 'name', '=', 'member' );
}
)->lists('id');
foreach(range(1, 120) as $index)
{
$start_date = '2015-12-31 00:00:00';
$end_date = '2010-01-01 00:00:00';
$min = strtotime($start_date);
$max = strtotime($end_date);
// Generate random number using above bounds
$val = rand($min, $max);
$weeks = rand(1, 52);
// Convert back to desired date format
$start = new DateTime(date('Y-m-d H:i:s', $val));
$end = $start->modify('+' . $weeks . ' weeks');
Vacation::create([
'user_id' => $faker->randomElement( $members ),
'title' => $faker->sentence(3),
'description' => $faker->sentence(10),
'start' => $start,
'end' => $end
]);
}
}
}
Does anyone has an idea what the problem with my code could be?
Thanks,
Daniel