ItSolutionStuff.com

Laravel Migration Add DateTime Column Example

By Hardik Savani • November 5, 2023
Laravel

Hi Developer,

This tutorial will give you an example of laravel migration add datetime column. In this article, we will implement a how to add datetime datatype in laravel migration. This post will give you a simple example of laravel migration datetime field. I explained simply about laravel migration datetime column. So, let's follow a few steps to create an example of laravel migration create datetime column.

Laravel migration provides dateTime() to add date data type in mysql table. in this example, we will create pages table with publish_date field with date datatype. so, let's see the simple migration code:

Example:

Here, we will create pages table with add date to data type.

Create new migration using following command:

php artisan make:migration create_pages_table

Now, You can update it as like the bellow:

database/migrations/migration_name.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*/

public function up(): void

{

Schema::create('pages', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->text('body');

$table->dateTime('publish_date')->nullable();

$table->timestamps();

});

}

/**

* Reverse the migrations.

*/

public function down(): void

{

Schema::dropIfExists('pages');

}

};

Now, you are ready to run migration command:

php artisan migrate

You will see the layout as like the below:

I hope it can help you...

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 Add Mediumblob Data Type using Laravel Migration?

Read Now →

Laravel Migration Remove Auto Increment Example

Read Now →

How to Change Column Position in Laravel Migration?

Read Now →

How to Migrate SQL File in Laravel Migration?

Read Now →

Laravel Migration Change Datatype Date to Timestamp Example

Read Now →

Laravel Migration Change Datatype String to Integer Example

Read Now →

How to Drop Soft Delete from Table using Laravel Migration?

Read Now →

Laravel Migration Add Column After Column Example

Read Now →

Laravel Migration Default Value Current Timestamp Example

Read Now →

How to Update Enum Value in Laravel Migration?

Read Now →

Laravel Migration Add Enum Column Example

Read Now →

Laravel Migration Custom Index Name Example

Read Now →

Laravel Migration Custom Foreign Key Name Example

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →