Laravel timestamps() doesn't create CURRENT_TIMESTAMP
When you define a migration using $table->timestamps(), you expect Laravel to handle the boilerplate for created_at and updated_at. However, if you run php artisan migrate:refresh --seed and find that your records are being inserted with 0000-00-00 00:00:00, the issue isn't with the Eloquent model or the seed logic itself. The problem lies in how MySQL interprets the default value provided by Laravel's migration builder during the schema creation phase.
Laravel's $table->timestamps() method generates a standard Timestamp column definition. In many environments, this translates to a SQL definition where the default value is set as a literal string: '0000-00-00 00:00:00'. This specific date acts as a sentinel value in older versions of MySQL or when certain modes are enabled, signaling that a timestamp has not yet been populated.
The seed operation you described uses DB::table('mytable')->insert([...]). Crucially, this raw SQL insert statement does not include the columns for created_at and updated_at. While standard Eloquent mass assignment handles these automatically by injecting the current time via the model's accessor or mutator, a direct DB::table call bypasses those traits. Consequently, the database relies entirely on the column definition's default value. Since the migration defined the default as '0000-00-00 00:00:00', that is exactly what gets stored when you omit the columns from your insert array.
To achieve the behavior where timestamps are automatically calculated upon insertion and update, you must explicitly define CURRENT_TIMESTAMP in your migration. You can override Laravel's default helper method to generate the correct SQL syntax. Instead of relying on $table->timestamps(), use $table->timestamp('created_at', ['nullable' => false])->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')). This ensures that both the initial creation and subsequent updates trigger a timestamp update without requiring you to specify the column in every single insert statement.
If your schema has already been created with the incorrect defaults, you can alter the existing columns directly within a migration file using Schema::table or Blueprint::timestamp. However, be aware that altering default values on an active table requires careful consideration of data integrity and potential application downtime if other systems depend on specific timestamp formats.
There is also the matter of MySQL modes. If your database runs in strict SQL mode (STRICT_TRANS_TABLES), inserting a row without specifying created_at when the default is '0000-00-00' might actually fail or behave unexpectedly depending on the server configuration and version. The explicit use of CURRENT_TIMESTAMP removes ambiguity and aligns with standard SQL practices for auto-updating fields.
For teams managing multiple environments, it is vital to check your .env file and database configuration settings to ensure that zero dates are not being implicitly converted or accepted as valid defaults in unexpected ways. If you need further guidance on schema migrations or best practices for Laravel development, resources like https://laravelcompany.com can provide additional context on managing database structures effectively.