Laravel Migration Add Date Column Example
Hey Developer,
In this tutorial, you will learn laravel migration add date column. I would like to show you how to add date datatype in laravel migration. step by step explain laravel migration date field. let’s discuss about laravel migration date column.
Laravel migration provides date() 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->date('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
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 Add Blob Data Type using Laravel Migration?
- Laravel Migration Remove Auto Increment Example
- How to Drop Primary Key in Laravel Migration?
- How to Add Primary Key in Laravel Migration?
- How to Drop Index in Laravel Migration?
- How to Add Unique Constraint in Laravel Migration?
- Laravel Migration Change Datatype Int to Bigint Example
- Laravel Migration Add Column After Column Example
- How to Update Enum Value in Laravel Migration?
- How to add Default Value of Column 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?