2014年11月18日火曜日

Laravel4 Migrations Tips

As we read programming books or homepages, we sometimes encounter the confusion because of the different explanations from authers to authers.

Laravel's Migrations is in that case. Let's me explain the best use of migrate:make command.

First, here are three lines for migrate:make command. You will see which is the best way to write seeing the results.

Laravel4 Formal Documentation

1. $ php artisan migrate:make create_users

2. $ php artisan migrate:make create_users --table=users
2-2. $ php artisan migrate:make create_users --table="users" //Double quotations are not required. This line shoud be like upper one.

Sorry, I made a mistake. 3-2 is wrong. 3 is a true line I intended. Originally, I wrote this blog in august.
3. $ php artisan migrate:make create_users --create=users
3-2. $ php artisan migrate:make create_users --create --table=users

The results for each command are like below.

The result for the first command line.
------------------------------
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration {
/**
* Run the migrations.
*
* @return void
*/
 public function up()
 {
  //
 }
/**
* Reverse the migrations.
*
* @return void
*/
 public function down()
 {
  //
 }
}
-------------------------------


The result for the second command line.
-------------------------------
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration {
/**
* Run the migrations.
*
* @return void
*/
 public function up()
 {
  Schema::table('users', function(Blueprint $table) {
  });
 }
/**
* Run the migrations.
*
* @return void
*/
 public function down()
 {
  Schema::table('users', function(Blueprint $table) {
  });
 }
}
-----------------------------


The result for the last command line.
-----------------------------
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration {
/**
* Run the migrations.
*
* @return void
*/
 public function up()
 {
  Schema::create('users', function(Blueprint $table) {
   $table->increments('id');
   $table->timestamps();
  });
 }
/**
* Reverse the migrations.
*
* @return void
*/
 public function down()
 {
  Schema::drop('users');
 }
}
------------------------------


The last line is the best for us. The migrate:make command make more job for us with the last. It's result makes us saving the coding time with less errors.

0 件のコメント:

コメントを投稿