Make migration command: did you know about parameters?

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!

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 89 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses