Tự học Laravel: (8) CREATE A MIGRATION

In order to create a simple blog from now on, we will create an Articles table for articles in the DB.

CREATE MIGRATION

Migration files are created using the artisan make: migration command.

php artisan make:migration migration_name 

The migration name is used in the file name of the migration to be created and the class name defined in it.

Well, let’s try it actually. Specifying the migration name as create_tablename_table generates a migration for table creation. Also, it is common to make the table name plural.

php artisan make:migration create_articles_table 

Migration files are created in the database/maigrations folder. The file name is timestamped to let the framework know the migration order.

database /
  Mig migrations
          20 2018_08_24_024919_create_articles_table.php
<?php //2018_08_24_024919_create_articles_table.php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateArticlesTable extends Migration
{
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

CreateArticlesTable class has been created that inherits from Migration class. The class name is the migration name specified at make:migration , converted to camel case .

  • The table creation process (Schema::create) is included in up(), and id and timestamp items are defined.
  • down() contains the process of dropping a table.

Now add the required title and body items to the Articles table.

<?php //2018_08_24_024919_create_articles_table.php
 
    // ...
    public function up()
    {
        Schema::create('articles', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');        
            $table->text('body');          
            $table->timestamps();
        });
    }
    // ...
}

Please refer to the following site for the detailed method of the item addition.
https://laravel.com/docs/master/migrations#columns

Now, run migration.

php artisan migrate

Migrating: 2018_08_24_024919_create_articles_table
Migrated: 2018_08_24_024919_create_articles_table

Use the sqlite3 command to check if the table has been created.

sqlite3 database/database.sqlite 

sqlite> .tables
articles migrations password_resets users
sqlite> .schema articles-indent
CREATE TABLE "articles" (
"id" integer not null primary key autoincrement,
"title" varchar not null,
"body" text not null,
"created_at" datetime not null,
"updated_at" datetime not null
);
sqlite> .q

An Articles table has been created!

TABLE CHANGE

Now let’s change the table by migration. Add the published_at field to the Article table.

There are two ways. The first method is to roll back once, add an item to the migration file, and then run migration again. The second method is to create a migration file for item addition and execute migration. Here, we will try the second method.

To create a file to modify the table, execute make:migration with the –table option.

php artisan make:migration add_published_at_to_articles_table --table=articles 
<?php //2018_08_24_031429_add_published_at_to_articles_table.php
 
<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class AddPublishedAtToArticlesTable extends Migration
{
    public function up()
    {
        Schema::table('articles', function (Blueprint $table) {
            //
        });
    }
 
    public function down()
    {
        Schema::table('articles', function (Blueprint $table) {
            //
        });
    }
}

A new migration file has been created. 
Both up() and down() contain table schema change processing (Schema::table).
Now add the published_at item. 
Up() favors the addition of an item, down() favors the deletion of an item.

<?php //2018_08_24_031429_add_published_at_to_articles_table.php
 
    // ...
    public function up()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->timestamp('published_at')->nullable();
        });
    }
 
    public function down()
    {
        Schema::table('articles', function(Blueprint $table)
        {
            $table->dropColumn('published_at');
        });
    }
}

Now, run migration.

php artisan migrate

Migrating: 2018_08_24_031429_add_published_at_to_articles_table
Migrated: 2018_08_24_031429_add_published_at_to_articles_table

Use the sqlite3 command to see if an item has been added to the table.

sqlite3 database/database.sqlite 

sqlite> .schema articles-indent
CREATE TABLE "articles" (
    "id" integer not null primary key autoincrement,
    "title" varchar not null,
    "body" text not null,
    "created_at" datetime not null,
    "updated_at" datetime not null,
    "published_at" datetime null
);          

sqlite> .q

Published_at is added to the Articles table!

SUMMARY

You can do the following things.

  • Create migration file
  • Create table
  • Table change

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.