To create migration, we can use this artisan command.
$ php artisan make:migration create_companies_table
Laravel will create a new migration file in database/migrations
directory.
Generated file is like this:
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
up()
method is used to create a new tables or add new columns to table. down()
method is used to rollback.
We can use --create
option when create migration to automatically generate base code.
$ php artisan make:migration create_companies_table --create=companies
Generated file is like this:
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('companies');
}
}