Javatpoint Logo
Javatpoint Logo

Generating Migrations

In the previous topic, we have acquired the knowledge of the migrations that are already available in the laravel project. Now, we will learn how to generate our own migration.

Steps to generate the Migration

Following are the steps to generate the Migration:

  • Open the Git bash window and type the command given below:'
    php artisan make:migration create_posts_table - -create="posts"
Laravel Generating Migrations

The above output shows that the migration table has been created with the name "create_posts_table". The '-create="posts"' Is the flag that creates the table with a name "posts". We follow the same rule to name the migration table as laravel did with the default migration table.

  • To view the migration table, open the directory "C:\xampp\htdocs\firstprojectproject\database\migrations".
Laravel Generating Migrations

The above output shows that the migration table with the name "create_posts_table" has been created successfully.

  • Open the migration file, the structure of this file is given below:

The above code is the newly generated class that contains the two methods, i.e., up() method and down() method. The up() method implements the create() method that contains two parameters, i.e., name of the table(posts) and the closure function. The closure function contains the Blueprint class with its object $table as a parameter. The two columns are created by Laravel; the first one is the auto-incremental column with a name 'id', and the second column is of type timestamp.

Suppose I added two more columns shown in the below code:

Now we will do the migration to run the above code. Open the Git bash window to run the migrate command.

Laravel Generating Migrations

The above command creates the migration table in a database.

Open the phpmyadmin to view the migration table.

Laravel Generating Migrations

The above output shows that the migration is created, and also the 'posts' table.

The structure of the 'posts' table is shown below:

Laravel Generating Migrations

Adding columns to the existing table using migration

I want to add new columns in a table that is already created. I do not want to move back to that file that has created the table, dropping the table, and then re-creating the table, and re-migrate it. So, I will create a new migration file that adds the new columns to an existing table.

Following are the steps to add the columns to an existing table:

  • Open the Git bash window and enter the command given below:
    php artisan make:migration add_column_admin_is_to_table -table="posts" where add_column_admin_is_to_table is the name of the migration file and -table="posts" is the flag which tells the laravel that we are working with the posts table.
Laravel Generating Migrations
  • The migration file is created in the xampp/htdocs/firstproject/database/migrations directory, where firstproject is the name of the laravel project.
Laravel Generating Migrations
  • Open the migration file and the structure of the newly created migration file is given below:
  • We add the following code in up() method:

The above code shows that we are adding new column, i.e., is_admin to the posts table.

  • Enter the migrate command in a Git bash window, i.e., php artisan migrate.
Laravel Generating Migrations
  • Open the phpMyAdmin to view the structure of the 'posts' table.
Laravel Generating Migrations

In the above output, we observe that the new column, i.e., is_admin, is created, and the type of the integer is unsigned. The unsigned integer means that this column does not contain any negative numbers.

  • If we want to apply some modifications to this column, then first, we need to rollback the migration. The command is php artisan migrate:rollback.
Laravel Generating Migrations
  • When we rollback the migration, then the structure of the 'posts' structure would look like:
Laravel Generating Migrations

The above output shows that the is_admin column has been removed from the table.

  • Suppose I want to provide the default value to the is_admin column, then it can be done as follows:
  • To add the is_admin column again to the posts table with a default value 0, run the migrate command.
Laravel Generating Migrations
  • Open the phpMyAdmin.
Laravel Generating Migrations

The above output shows that the is_admin column is added to the posts table with a default value 0.






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA