Home > OS >  carbon generated datetime not stored correctly into the database
carbon generated datetime not stored correctly into the database

Time:03-09

I have a tv_shows table where i'm trying to store the start and end datetime of each tv show.

this is my migration:

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();
        });

this is the factory:

 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,
        ];
    }

this is the TvShow model:

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',
    ];
}

if I run Tvshow::factory()->count(4)->make(), I get:

 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,
       },
     ],
   }

as you can see in almost all instances the year is wrong it should be 2022 but there is 2333, 3010 and so on. where am I wrong in my code?

CodePudding user response:

When you do:

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

Faker may return anything up to PHP's mt_getrandmax as the randomNumber(). On my system, that's 2147483647.

2147483647 hours is nearly 250,000 years.

If you want realistic dates, consider using one of Faker's date and time functions instead.

  • Related