Laravel 播种:日期操作无效

Laravel Seeding: date manipulation not working

我有一个要播种的枢轴 table。除了 PK 和 FK,table 还包含另外两列:Arrival & Departure(类型:时间戳)。 我正在使用 Carbon 随机填充前面的列。 这是我的代码:

$faker = Faker::create();
for( $i=0 ; $i<55500 ; $i++){
   $nowDt = Carbon::now();
   $nowDt->timezone = 'Europe/London';
   $nowDt->addMinutes($faker->numberBetween(25,55));
   $this->command->info("ARRIVAL : ". $nowDt);
   $departure = $nowDt->addMinutes($faker->numberBetween(35,45));
   $this->command->info("DEPARTURE : ". $departure);
   $region->entities()->attach($random_entity,[
            'arrival'       => $nowDt,
            'departure'     => $departure,
            'created_at'    => Carbon::now(),
            'updated_at'    => Carbon::now()
        ]);
   }

奇怪的是,输出到控制台的信息如下:

ARRIVAL : 2015-06-11 08:24:29
DEPARTURE : 2015-06-11 09:13:29

但是当我查看插入的数据时,到达和离开的值完全相同。

ARRIVAL : 2015-06-11 08:24:29
DEPARTURE : 2015-06-11 08:24:29

我做错了什么?

这是因为你操作的是同一个日期对象。如果您需要将日期和 return 作为保留当前对象的新对象进行操作,请在操作前使用 copy 方法。否则它将 return 引用您正在操作的同一对象。

更改此行

$nowDt->addMinutes($faker->numberBetween(35,45));

$nowDt->copy()->addMinutes($faker->numberBetween(35,45));