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 and laravel 9 version.

so let’s see bellow simple examples:

Create Migration Command:

php artisan make:migration create_items_table

.medrectangle-3-multi-157{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

Example 1: using useCurrent() – Best Way

database/migrations/2021_04_07_125911_create_items_table.php

<?php

use IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateSupportFacadesSchema;

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

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

Example 2: using CURRENT_TIMESTAMP

database/migrations/2021_04_07_125911_create_items_table.php

<?php

use IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateSupportFacadesSchema;

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…

1 thought on “Laravel Migration Default Value Current Timestamp Example”

Leave a Comment