Skip to main content
Tutorial Free

Make migration command: did you know about parameters?

September 05, 2016
2 min read
Quick tip for you guys today. Maybe you already know that, but for some of you this might be useful. Did you know that make:migration command has optional parameters --create and --table? Basically, those options help you generate wrappers like Schema::create() and Schema::table(). Let's see the examples:

1. Simple migration command to create a table:

php artisan make:migration create_projects_table
Result (just the main parts):
class CreateProjectsTable extends Migration
{
    public function up()
    {
        //
    }

    public function down()
    {
        //
    }
}
So you need to fill in all the code for the methods yourself. Now, see the parameters:

2. Parameter --create

php artisan make:migration create_projects_table --create=projects
Result:
class CreateProjectsTable extends Migration
{
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('projects');
    }
}
See, table creation is already pre-filled for you, with ID and timestamps fields included, and down() method pretty much ready.

3. Parameter --table

php artisan make:migration add_email_field_to_projects_table --table=projects
Result:
class AddEmailFieldToProjectsTable extends Migration
{
    public function up()
    {
        Schema::table('projects', function (Blueprint $table) {
            //
        });
    }

    public function down()
    {
        Schema::table('projects', function (Blueprint $table) {
            //
        });
    }
}
This option is for editing the table - most often for adding new columns. It generates the table structure, but you have to fill in the field details inside of the Closure function. Basically, that's it - quick tip, hope that helps!

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.