碳生成的日期时间未正确存储到数据库中

carbon generated datetime not stored correctly into the database

我有一个 tv_shows table,我试图在其中存储每个电视节目的开始和结束日期时间。

这是我的迁移:

Schema::create('tv_shows', function (Blueprint $table) {
            $table->id();

            $table->unsignedBigInteger('channel_id');
            $table->foreign('channel_id')->references('id')->on('channels')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->unsignedBigInteger('tv_show_detail_id');
            $table->foreign('tv_show_detail_id')->references('id')->on('tv_show_details')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->dateTime('starting_at');
            $table->dateTime('ending_at');
            $table->timestamps();
        });

这是工厂:

 public function definition()
    {
        $startingAt = Carbon::today()->copy()->addHours($this->faker->randomNumber());

        return [
            'starting_at' => $startingAt->toDateTimeString(),
            'ending_at' => $startingAt->copy()->addHour()->toDateTimeString(),
            'channel_id' => Channel::factory()->create()->id,
            'tv_show_detail_id' => TvShowDetail::factory()->create()->id,
        ];
    }

这是TvShow型号:

class TvShow extends Model
{
    use HasFactory;

    protected $casts = [
        'created_at' => 'datetime:Y-m-d',
        'starting_at' => 'datetime:Y-m-d H:i:s',
        'ending_at' => 'datetime:Y-m-d H:i:s',
    ];
}

如果我 运行 Tvshow::factory()->count(4)->make(),我得到:

 Illuminate\Database\Eloquent\Collection {#3593
     all: [
       App\Models\TvShow {#3611
         starting_at: Carbon\Carbon @11472638400 {#3570
           date: 2333-07-22 04:00:00.0 UTC (+00:00),
         },
         ending_at: Carbon\Carbon @11472642000 {#3561
           date: 2333-07-22 05:00:00.0 UTC (+00:00),
         },
         channel_id: 1079,
         tv_show_detail_id: 1079,
       },
       App\Models\TvShow {#3617
         starting_at: Carbon\Carbon @1669834800 {#3560
           date: 2022-11-30 19:00:00.0 UTC (+00:00),
         },
         ending_at: Carbon\Carbon @1669838400 {#3589
           date: 2022-11-30 20:00:00.0 UTC (+00:00),
         },
         channel_id: 1080,
         tv_show_detail_id: 1080,
       },
       App\Models\TvShow {#3635
         starting_at: Carbon\Carbon @32836662000 {#3613
           date: 3010-07-21 23:00:00.0 UTC (+00:00),
         },
         ending_at: Carbon\Carbon @32836665600 {#3610
           date: 3010-07-22 00:00:00.0 UTC (+00:00),
         },
         channel_id: 1081,
         tv_show_detail_id: 1081,
       },
       App\Models\TvShow {#3645
         starting_at: Carbon\Carbon @1099374872400 {#3590
           date: 36807-10-21 05:00:00.0 UTC (+00:00),
         },
         ending_at: Carbon\Carbon @1099374876000 {#3607
           date: 36807-10-21 06:00:00.0 UTC (+00:00),
         },
         channel_id: 1082,
         tv_show_detail_id: 1082,
       },
     ],
   }

如您所见,在几乎所有情况下,年份都是错误的,应该是 2022 年,但也有 2333、3010 等等。我的代码哪里错了?

当你这样做时:

$startingAt = Carbon::today()->copy()->addHours($this->faker->randomNumber());

Faker 可能 return 任何高达 PHP 的 mt_getrandmax 作为 randomNumber()。在我的系统上,这是 2147483647。

2147483647小时将近25万年

如果您想要真实的约会,请考虑使用 Faker 的 date and time functions 之一。