Inserting created_at data with Laravel

Inserting created_at data with Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why You Shouldn't Manually Set created_at in Laravel

Attempting to manually inject a created_at value into a database record using a raw string or a calculated timestamp is an anti-pattern that directly conflicts with Laravel's built-in timestamp mechanics. The error you are encountering—InvalidArgumentException originating from the Carbon library—is not a bug; it is a safety mechanism preventing data corruption.

When your code executes $post->created_at = $create_dt;, you are assigning a string (e.g., "2023-10-27 14:30:00") to an Eloquent model property. Under the hood, Laravel casts this property to a Carbon instance. The Carbon library attempts to parse your string to determine the timezone and format. If the string does not strictly match the expected ISO-8601 format or lacks necessary components like the timezone offset, Carbon throws an exception because it cannot reliably construct the object. Furthermore, mixing manual string manipulation with Eloquent's automatic timestamp handling creates a race condition where you lose control over data integrity.

The Automatic Timestamp Mechanism

Laravel includes two special columns, created_at and updated_at, specifically designed to track record history without developer intervention. When these columns exist in your migration file, Laravel automatically manages them using the Timestamps trait.

You do not need to set $timestamps to false if you want automatic updates; in fact, disabling it removes the ability for Laravel to update the updated_at column automatically as well. The trait handles both columns simultaneously. If you only care about tracking creation time and want to manually manage updates later, you can configure this by setting $timestamps = ['created_at'] within your Model class, though this is rarely necessary for standard CRUD operations.

Using Carbon Instances Instead of Strings

If your specific requirement is to enforce a creation date that differs from the current system time (for instance, back-dating a post), you must respect the Carbon parsing rules. You cannot simply pass a formatted string like "2023-10-27 14:30:00" directly if your application operates in UTC but the database expects a specific timezone offset.

To solve this without disabling timestamps, you should instantiate a Carbon object with the correct timezone context before assigning it to the model property. Laravel's date helper returns a string by default, but you can force it to return a Carbon instance that handles parsing safely:

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;

// Calculate the target date from POST data
$targetDate = Carbon::createFromFormat('Y-m-d H:i:s', $_POST['post_date'] . ' ' . $_POST['post_time']);

// Assign to the model
$post = new Post();
$post->name = $_POST['name'];
$post->created_at = $targetDate;

// Save the record
$post->save();

However, even with Carbon instances, manually overriding created_at is generally discouraged unless you have a strict business requirement for it. The standard workflow assumes that the moment a record is saved is its creation moment. Overriding this value can lead to confusion during data auditing and reporting, where timestamps are expected to reflect the actual insertion time relative to server logic.

Migration Requirements

Before attempting any manual assignment, ensure your database schema supports these columns. In your migration file, define the columns as follows:

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->timestamp('created_at')->useCurrent();
    $table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();
});

If you attempt to insert a timestamp into a column defined without these attributes, Laravel will either ignore the assignment or throw a different error depending on your strict mode settings. The useCurrent() directive instructs the migration runner to set the default value to the current time when no value is provided, but it does not prevent you from overriding it if a valid Carbon instance is passed.

Final Consideration

If your goal is simply to ensure that records have a creation timestamp without worrying about back-dating, remove the manual assignment entirely. Let Laravel handle the created_at column automatically:

$post = new Post();
$post->name = $_POST['name'];
// Do not touch $post->created_at
$post->save();

This approach guarantees that the timestamp reflects the exact moment the record entered your application's lifecycle, providing an accurate audit trail. Disabling timestamps is a heavy-handed solution that removes this safety net for both creation and update events. Stick to Carbon instances if you must override values, but default to automatic management whenever possible.

Our network