How to Create a Migration Script in Laravel using PHP Artisan Command

Posted on

This is another article related to the Laravel web-based application development specifically discuss on how to create a migration script in Laravel itself. Laravel web-based application development in its utility provided and in the context of this article, it is the PHP Artisan command, is able to be utilized to create a migration script.

Migration script is needed especially at the time generating and modifying tables is a necessary things to be done. It can be executed for creating a migration script which can be edited and modified accordingly based on needs.

The following is the pattern for achieving the purpose of creating a migration script :

php artisan make:migrate [script_file_name_part]

For an example :

user@hostname:/var/www/html/laravel$ php artisan make:migration role
Created Migration: 2017_11_05_120717_role
user@hostname:/var/www/html/laravel$ php artisan make:migration user_role
Created Migration: 2017_11_05_120721_user_role
user@hostname:/var/www/html/laravel$

The above commands are the output of generating migration script file. There are two migration script files generated and it can be checked by accessing the folder named ‘database/migrations’ as follows :

user@hostname:/var/www/html/laravel/database/migrations$ pwd
/var/www/html/laravel-support-ticket/database/migrations
user@hostname:/var/www/html/laravel/database/migrations$ ls -al
total 48
drwxrwxr-x 3 www-data user 4096 Nov 5 21:04 .
drwxrwxr-x 5 www-data user 4096 Aug 30 16:55 ..
...
-rw-rw-r-- 1 user user 556      Nov 5 21:04 2017_11_05_120717_role.php
-rw-rw-r-- 1 user user 579      Nov 5 20:56 2017_11_05_120721_user_role.php
user@hostname:/var/www/html/laravel/database/migrations$ tree .
.
...
├── 2017_11_05_120717_role.php
├── 2017_11_05_120721_user_role.php
...

1 directory, 11 files
user@hostname:/var/www/html/laravel/database/migrations$

As it is shown in the above output command, there are two files created on behalf of each php artisan make:migrate command and each of it has the file where part of the name is retrieved from the parameter given upon executing the php artisan command. It is the ‘role’ and the ‘user_role’ where it is becoming part of the migration script file names which each of them are ‘2017_11_05_120717_role.php’ and ‘2017_11_05_120721_user_role.php’ respectively.

The content of those two generated files are the same. It is meant to be edited further based on the needs of every migration script file since those files itself, each of them represent the table which is going to be worked on. Below are the contents of a newly generated migration script file :

<?php

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

class Role extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

Look out for the above migration script file content carefully. There is a specific part which is going to be different from one file to another. Those parts are :

class Role extends Migration {

The class name is taken from the script name.

 

 

One thought on “How to Create a Migration Script in Laravel using PHP Artisan Command

Leave a Reply