
Inside this post, we will discuss creating migration, adding a comment to a new column, and adding a comment to existing columns.
For this article, I am assuming that you already have installed laravel and configured your database. Without database configuration migration won’t work. So if you have not done that yet, please do now.
First, we will start with How to create Migration.
Open vscode if you are a fan. But not an issue if you are using the terminal, just type the below command to create a migration.
php artisan make:migration create_posts_table
Note: Table name should be plural form, like posts, users, products.
Okay, we just created migration, next we need to write some code to add a column in migration. Let’s do that,
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->string('content');
$table->string('view')->comment("number of view counter");
$table->timestamps();
});
}
In the above, we added comments to a column, now anyone can know what that column is for. It’s easier to understand why the column is used if we add comments to columns.
Let’s see, how to add a comment to the existing column, let’s create migration for altering the table
php artisan make:migration alter_posts_table
The above command will create a new migration file to alter the existing table. Let’s open up the file and write some code.
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('title')->comment("post title")->change();
});
}
That’s it. Hope this helps.


Leave a Reply