我正在尝试在 Laravel Factory 中生成唯一的日期序列。例如:
2019-05-04
2019-05-05
2019-05-06
...
2019-07-04
我尝试过这个,每个日期出现 0 - n 次。
$faker->unique()->dateTimeBetween('-7 days', '+2 months')->format('Y-m-d'),
你为什么不尝试一下这个最随机的日期时间,如下所示:
'created_at' => $faker->dateTimeThisMonth(),
并且在一段时间内,正如文档所说,你必须像下面这样做:
$this->faker->unique()->dateTimeBetween($startDate = '-30 years', $endDate = 'now', $timezone = null) // DateTime('2003-03-15 02:00:49', 'Africa/Lagos')
这是它的链接:
https://github.com/fzaninotto/Faker#fakerproviderdatetime
所以在你的情况下
$this->faker->unique()->dateTimeInInterval($startDate = '-7 days', $interval = '+ 5 days', $timezone = null)