0

I recently discovered, that the columns created_at and updated_at created by the timestamps() function use the TIMESTAMP type. I know that there are many similarities, but the DATETIME type has a much higher range beyond the year 2038. So why does Laravel not bother to use the DATETIME type for these default columns?

Would there be a workaround to just create these columns manually while keeping the functionality that Laravel automatically updates the columns?

Leander Hass
  • 162
  • 2
  • 3
  • 13
  • I am not sure in keeping laravel functionality but you can try these in migration: `$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));` and `$table->dateTime('updated_at')->default(DB::raw('CURRENT_TIMESTAMP'))->onUpdate(DB::raw('CURRENT_TIMESTAMP'));` – Mr. Kenneth Aug 09 '23 at 00:28
  • more info in this laracast: https://laracasts.com/discuss/channels/laravel/migrations-timestamp-vs-datetime-vs-date-vs-timestamps – Mr. Kenneth Aug 09 '23 at 00:28
  • Could also probably do: `$table->dateTime('created_at')->useCurrent();` and `$table->dateTime('updated_at')->nullable()->useCurrentOnUpdate();` – Paul T. Aug 09 '23 at 02:20
  • Thanks for the link and the recommendation! – Leander Hass Aug 09 '23 at 20:27
  • Laravel can use timestamp for things like created_at, updated_at because it knows that the value stored will be near real time and not before 1970 or after 2038 – Zohaib Aug 10 '23 at 19:55
  • @Zohaib Well, but this basically puts you up until 2038 where you have to migrate anyway to dateTime columns – Leander Hass Aug 21 '23 at 01:11

0 Answers0