ItSolutionStuff.com

Laravel Migration Default Value Current Timestamp Example

By Hardik Savani β€’ April 16, 2024
Laravel

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...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Change Column Length using Laravel Migration?

Read Now β†’
β˜…

How to Update Enum Value in Laravel Migration?

Read Now β†’
β˜…

Laravel Migration Enum Default Value Example

Read Now β†’
β˜…

Laravel Migration Add Enum Column Example

Read Now β†’
β˜…

How to Rollback Migration in Laravel?

Read Now β†’
β˜…

Laravel Migration Add Comment to Column Example

Read Now β†’
β˜…

Laravel Migration Custom Index Name Example

Read Now β†’
β˜…

How to Add Index in Laravel Migration?

Read Now β†’
β˜…

How to Add Foreign Key in Laravel Migration?

Read Now β†’
β˜…

Laravel Migration - How to Add New Column in Existing Table ?

Read Now β†’
β˜…

How to Change Table Name using Laravel Migration?

Read Now β†’
β˜…

How to Remove Column from Table in Laravel Migration?

Read Now β†’
β˜…

How to Change Column Name and Data Type in Laravel Migration?

Read Now β†’
β˜…

How to Create Table using Migration in Laravel?

Read Now β†’