【问题标题】:Laravel Seeding: date manipulation not workingLaravel 播种:日期操作不起作用
【发布时间】:2015-06-11 08:13:00
【问题描述】:

我有一个要播种的数据透视表。除了 PK 和 FK,该表还包含另外两列: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

我在这里做错了什么?

【问题讨论】:

    标签: php database postgresql laravel-5 php-carbon


    【解决方案1】:

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

    改变这一行

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 2021-02-11
      相关资源
      最近更新 更多