Inserting created_at data with Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Inserting a created_at data in your database table through Laravel can seem challenging at first, but it isn't complicated once you understand how to approach it. In this blog post, we will walk you through the process of making sure you insert the correct time-stamp into your database with ease while maintaining clean and manageable code.
Start by creating a proper migration for your model. This is essential to ensure that Laravel can handle the data type correctly. For instance, if you are using timestamps:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->text('body');
$table->string('title');
$table->timestamps();
});
This migration generates a table with the necessary columns, including timestamps for both created_at and updated_at. If you want to be more specific about the format of your timestamps (for example, always using Y-m-d H:i:s A), simply change it in the migration after generating it.
Now to insert data with a POST request, use Laravel's model binding feature:
$create_dt = Carbon::now('America/Los_Angeles'); // Set your timezone if needed
Request::setTrustsRecursion(true);
Request::merge($_POST + [ 'post_date' => $create_dt->format("Y-m-d"), 'post_time' => $create_dt->format("H:i:s") ]);
$name = $_REQUEST['name'];
$post = Post::findOrFail(1); // Replace with your desired post ID
$post->update([
'title' => $_POST['title'],
'body' => $_POST['body'],
'name' => $name,
'created_at' => $create_dt,
]);
In this example, we create the required timestamp and use model binding to merge it into a POST request. Additionally, we update the existing post with details provided in the POST data along with the created_at timestamp.
If using Eloquent models, you might have set $timestamps = true to let Laravel handle the timestamps automatically. While there is nothing wrong with this approach, sometimes you may want more control over your data, in which case, you can use the following technique:
$post = new Post();
$post->name = $name;
...
$post->created_at = $create_dt; // Set created_at directly to your desired timestamp
$post->save();
In this case, we create a new post instance and add the data we need. Then, we set created_at manually before saving the model. This approach may be suitable if you have more control over your database schema and need to handle specific timestamp formats or timezones.
However, disabling timestamps globally in Laravel is not a good idea. Doing so will disable automatic handling of updated_at, which can cause problems with other functionality like the Eloquent query builder's soft deletes and versioned models.