How to create database migration in Laravel 4.2 - BOOKC -->

How to create database migration in Laravel 4.2

Tuesday, April 19, 2016


To create and modify table in laravel we can use migration, it's quite simple and easy to use, there is dedicated artisan command for create, run and rollback the migration.

Let's try create two new table called users and roles, but before we do that, let's configure the database config file in laravel so that laravel can connect to the database, which in this case mysql.

You can find the database configuration file under the app/config folder on your laravel 4.2 project folder, look for file called database.php, look for 'connections' section:

'connections' => array(

'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),

'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'just_for_testing',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),


'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'forge',
'username' => 'forge',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),

'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'prefix' => '',
),

),

Alright next, let's run the artisan command for creating the migration file, which later will be stored on app/database/migrations/ folder.

php artisan migrate:make create_table_users --create=users

NOTE: if you get error permission denied when running command above, try add sudo like this:

sudo php artisan migrate:make create_table_users --create=users

Now, open the migration file and then add this codes for creating table called 'users' using laravel schema builder.

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableUsers extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table)
{
$table->bigIncrements('user_id');
$table->bigInteger('role_id')->unsigned()->nullable();
$table->string('name', 255)->nullable();
$table->string('password', 255)->nullable();
$table->enum('gender', array('male','female'))->nullable();
$table->string('status', 10)->default('active')->nullable();
$table->timestamps();
$table->index(array('user_id'), 'user_id_idx');
$table->index(array('role_id'), 'role_id_idx');
$table->index(array('name'), 'name_idx');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}

}

At this point, the table is not yet created because the migration file is not run yet, to run the migration use this artisan command:

php artisan migrate

Okay, i think i need to create another table called roles, let's run the artisan command again to create the migration file called create_table_roles.

php artisan migrate:make create_table_roles --create=roles

Enter this code for migration file create_table_roles:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableRoles extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('roles', function(Blueprint $table)
{
$table->bigIncrements('role_id');
$table->string('role_name', 50);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('roles');
}

}

Don't forget to run the migration with 'php artisan migrate' or 'sudo php artisan migrate' if you get permission denied error.


Now take a look at your database, you should have two new tables, table users and table roles. Next article i will show you how to create models from the two tables that we just created.