How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?
How to Set the Default Value of a Timestamp Column in Laravel Migrations
š· by Clay Banks on Unsplash
So you want to set the default value of a timestamp column to the current timestamp using Laravel Migrations? You're not alone! Many developers face this challenge when working with Laravel's powerful Schema Builder.
In this blog post, we'll address this common issue, provide easy solutions, and get you up and running in no time. Let's dive in!
The Problem
As you may have noticed, the timestamps()
function in Laravel sets default values of 0000-00-00 00:00
for both created_at and updated_at columns. But what if you need the columns to default to the current timestamp when a record is created or updated? You're in luck, because there are a couple of straightforward ways to achieve this.
Solution 1: Modifying the Migration File
The first solution involves modifying the migration file itself. Open the migration file that corresponds to your desired table and locate the timestamps()
function call. By default, it looks like this:
$table->timestamps();
To set the default values to the current timestamp, you can manually modify the created_at
and updated_at
columns during the creation of the table. To do this, replace the timestamps()
function call with the following code:
$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
With this modification, the created_at
column will default to the current timestamp when a record is created, and the updated_at
column will be updated to the current timestamp whenever a record is updated.
Solution 2: Modifying the Model
The second solution involves modifying the corresponding model to automatically set the default values when a new record is created or updated. Laravel provides a convenient way to do this using Eloquent's bootable trait functionality.
Open the model file associated with your table and add the following code to the class:
use Illuminate\Database\Eloquent\Model;
class YourModel extends Model
{
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->created_at = $model->freshTimestamp();
$model->updated_at = $model->freshTimestamp();
});
static::updating(function ($model) {
$model->updated_at = $model->freshTimestamp();
});
}
}
With this modification, the created_at
and updated_at
columns will automatically be set to the current timestamp whenever a new record is being created or updated.
Conclusion
Setting the default value of a timestamp column to the current timestamp with Laravel Migrations doesn't have to be a headache. With the solutions provided in this blog post, you can easily achieve the desired behavior.
Whether you choose to modify the migration file or the model, make sure to select the solution that best fits your project's needs.
Now it's your turn! Have you faced any challenges when setting default values for timestamp columns in Laravel? How did you handle them? Share your experiences in the comments below!
ššš