将日期时间字符串传递给 laravel 模型工厂选择随机日期
Passing datetime string to laravel model factory chooses random date
我有一个 table 个错误,一个 table 个操作和一个历史记录 table。
每次对错误执行操作时,我都想将其记录在我的历史记录中 table。
我正在尝试将一些虚拟数据植入数据库。这就是我正在做的:
public function run()
{
$bugs = App\Bug::all();
foreach ($bugs as $bug) {
//Add a history entry for the bug
factory(App\History::class)->create([
'action_id' => App\Action::where('name', '=', 'create')->first()->id,
'user_id' => $bug->project->users->random()->id,
'bug_id' => $bug->id,
'created_at' => $bug->created_at,
'updated_at' => $bug->updated_at
]);
//Here I perform some random actions and save the history of the action
for ($i = 0; $i < 9; $i ++) {
$createdDate = $bug->created_at->addDays(mt_rand(1,6))->addHours(mt_rand(1,23))->toDateTimeString();
//This is the only action I'm having trouble with
factory(App\History::class, 1)->create([
'created_at' => $createdDate,
'action_id' => App\Action::where('name', '=', 'comment')->first()->id,
])->each(function ($history) {
$history->comment()
->save(factory(App\Comment::class)->make());
});
}
}
}
在我的虫子模型工厂里我有
$factory->define(App\Bug::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'description' => $faker->sentence,
'project_id' => $faker->randomNumber(),
'created_at' => $faker->dateTimeBetween('-1 years'),
];
});
我希望上面的 created_at 字段比 bug 的 created_at 字段晚 1-5 天和 1-23 小时存储一个历史条目。
我现在看到的是过去一年内的随机事件。好像我在做这个
$faker->dateTimeBetween('-1 years')
所以有时我的历史记录 table 是创建条目之前的条目。
我已经在日期中进行了硬编码并且它正确地存储了它。
与有关。由于 PHP 5+
"objects are passed by references by default"
通过克隆它们,您将获得一个新的不同实例。因此,这应该适合您:
$createdDate = $bug->created_at->copy()->addDays(mt_rand(1,6))->addHours(mt_rand(1,23))->toDateTimeString();
我有一个 table 个错误,一个 table 个操作和一个历史记录 table。
每次对错误执行操作时,我都想将其记录在我的历史记录中 table。
我正在尝试将一些虚拟数据植入数据库。这就是我正在做的:
public function run()
{
$bugs = App\Bug::all();
foreach ($bugs as $bug) {
//Add a history entry for the bug
factory(App\History::class)->create([
'action_id' => App\Action::where('name', '=', 'create')->first()->id,
'user_id' => $bug->project->users->random()->id,
'bug_id' => $bug->id,
'created_at' => $bug->created_at,
'updated_at' => $bug->updated_at
]);
//Here I perform some random actions and save the history of the action
for ($i = 0; $i < 9; $i ++) {
$createdDate = $bug->created_at->addDays(mt_rand(1,6))->addHours(mt_rand(1,23))->toDateTimeString();
//This is the only action I'm having trouble with
factory(App\History::class, 1)->create([
'created_at' => $createdDate,
'action_id' => App\Action::where('name', '=', 'comment')->first()->id,
])->each(function ($history) {
$history->comment()
->save(factory(App\Comment::class)->make());
});
}
}
}
在我的虫子模型工厂里我有
$factory->define(App\Bug::class, function (Faker\Generator $faker) {
return [
'name' => $faker->word,
'description' => $faker->sentence,
'project_id' => $faker->randomNumber(),
'created_at' => $faker->dateTimeBetween('-1 years'),
];
});
我希望上面的 created_at 字段比 bug 的 created_at 字段晚 1-5 天和 1-23 小时存储一个历史条目。
我现在看到的是过去一年内的随机事件。好像我在做这个
$faker->dateTimeBetween('-1 years')
所以有时我的历史记录 table 是创建条目之前的条目。
我已经在日期中进行了硬编码并且它正确地存储了它。
与有关。由于 PHP 5+
"objects are passed by references by default"
通过克隆它们,您将获得一个新的不同实例。因此,这应该适合您:
$createdDate = $bug->created_at->copy()->addDays(mt_rand(1,6))->addHours(mt_rand(1,23))->toDateTimeString();