Search

Laravel Artisan Generator Command: The make:migration Command

December 7, 2016 —John Koster

The make:migration command is used to generate a new migration class. Generated migrations are (by default) stored in the database/migrations directory. The make:migration defines a few parameters and options that can be used to customize the generated migration. The following table details these options:

Option Default Description
name None. Must be supplied. The name of the new migration file.
--create= None. The name of the table to be created.
--table= None. Will assume the value of --create no value is set for --table and a value exists for --create. The name of the table to be modified.
--path= database/migrations/ The directory to store the new migration file in.

The --create and --table options generally will not be used together. Using the --create option the new migration will be structured to create a new database table while using the --table will generate a migration for modifying an existing table with the value set. The behavior of the --create option will take precedence if both options are supplied.

The following examples demonstrate how to call the make:migration command and the example files that are produced.

The following example will simply generate an empty migration implementation:

1php artisan make:migration create_drinks_table

the resulting PHP file would look similar to the following:

1<?php
2 
3use Illuminate\Database\Schema\Blueprint;
4use Illuminate\Database\Migrations\Migration;
5 
6class CreateDrinksTable extends Migration
7{
8 /**
9 * Run the migrations.
10 *
11 * @return void
12 */
13 public function up()
14 {
15 //
16 }
17 
18 /**
19 * Reverse the migrations.
20 *
21 * @return void
22 */
23 public function down()
24 {
25 //
26 }
27}

The following example will generate a migration to create a new database table named drinks:

1php artisan make:migration create_drinks_table --create=drinks

the resulting PHP file would look similar to the following:

1<?php
2 
3use Illuminate\Database\Schema\Blueprint;
4use Illuminate\Database\Migrations\Migration;
5 
6class CreateDrinksTable extends Migration
7{
8 /**
9 * Run the migrations.
10 *
11 * @return void
12 */
13 public function up()
14 {
15 Schema::create('drinks', function (Blueprint $table) {
16 $table->increments('id');
17 $table->timestamps();
18 });
19 }
20 
21 /**
22 * Reverse the migrations.
23 *
24 * @return void
25 */
26 public function down()
27 {
28 Schema::drop('tacos');
29 }
30}

The following example will generate a migration to alter a database table named drinks:

1php artisan make:migration add_name_to_drinks_table --table=drinks

the resulting PHP file would look similar to the following:

1<?php
2 
3use Illuminate\Database\Schema\Blueprint;
4use Illuminate\Database\Migrations\Migration;
5 
6class AddNameToDrinksTable extends Migration
7{
8 /**
9 * Run the migrations.
10 *
11 * @return void
12 */
13 public function up()
14 {
15 Schema::table('drinks', function (Blueprint $table) {
16 //
17 });
18 }
19 
20 /**
21 * Reverse the migrations.
22 *
23 * @return void
24 */
25 public function down()
26 {
27 Schema::table('drinks', function (Blueprint $table) {
28 //
29 });
30 }
31}

The following example would generate an empty migration implementation in a different directory than the configured default:

1php artisan make:migration create_drinks_table --path=database/setup_migrations

When using the --path option the directory must already exist. The make:migration command will not create the directory for you and will fail with an ErrorException.

the resulting PHP file would look similar to the first example, but just in a different directory.

Running the make:migration command multiple times will not generate an error when the command is ran. A new migration file (with a different timestamp prefix) will be generated each time. Be aware that with most database engines migrations will fail when attempting to create a table that already exists.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.