Laravel Migration Default Value Current Timestamp Example
Hey Artisan,
This post is focused on laravel migration default value current timestamp. let’s discuss about laravel migration default current timestamp. This tutorial will give you a simple example of laravel migration timestamp default value. I’m going to show you about how to set default current timestamp in laravel migration. Here, Create a basic example of laravel migration default value current timestamp.
Laravel migration provides a useCurrent() and default() where you can set the default value current timestamps of that column. here I will give you simple tow examples of how to add default current timestamps, boolean, current time, etc. you can easily set with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
so let's see bellow simple examples:
Create Migration Command:
php artisan make:migration create_items_table
Example 1: using useCurrent() - Best Way
database/migrations/2021_04_07_125911_create_items_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->text('body');
$table->boolean('is_active');
$table->timestamp('creation_date')->useCurrent();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('items');
}
}
Now, you can run migration:
php artisan migrate
Example 2: using CURRENT_TIMESTAMP
database/migrations/2021_04_07_125911_create_items_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use DB;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->text('body');
$table->boolean('is_active');
$table->timestamp('creation_date')->default(DB::raw('CURRENT_TIMESTAMP'));;
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('items');
}
}
Now, you can run migration:
php artisan migrate
you can use as you need.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Change Column Length using Laravel Migration?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Enum Default Value Example
- Laravel Migration Add Enum Column Example
- How to Rollback Migration in Laravel?
- Laravel Migration Add Comment to Column Example
- Laravel Migration Custom Index Name Example
- How to Add Index in Laravel Migration?
- How to Add Foreign Key in Laravel Migration?
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Create Table using Migration in Laravel?