Database Migration in Laravel

Author - Maniraj Singh

Database Migration in Laravel

Migrations allow us to easily share the database schema of your website. There are only a few commands to run to create database tables. If, we do it manually there is some chances of getting an error and it’s also time consuming process.
Directory structure where the new migration will be placed database/migrations directory. Each migration file starts with timestamp followed by the name given by you. It allows user to determine the order of the migration.
The –table and –create options used to indicate the name of the table.
Migration Structure:-
A migration class contains two methods up and down. The up method is used to add columns in the table and the down method should reverse the operation performed by the up method.
Syntax to add columns in the dummy table by using specific type of column.

function up(){
		Schema::create('customers', function (Blueprint $table){
			$table->increments('id');
			$table->string('name');
			$table->string('email')->unique();
			$table->string('username')->nullable()->unique();
			$table->string('password');
			$table->rememberToken();
			$table->timestamps();
			});
	}

 function down(){
		Schema::dropIfExists('customers');
 }

Commands to create Migration :-

Migration could be created by two commands:-

1) php artisan make:migration create_customers_table
or
2) php artisan make:migration create_customers_table –create=customers

If, migration is creating by using first command you can view it in the below screenshot.
Command used : php artisan make:migration create_customers_table
Database Migration in Laravel

If, migration is done by using second command you can view it in the below screenshot. The –table and –create options may also be used to indicate the name of the table.
Command : php artisan make:migration create_customers_table –create=customers

Database Migration in Laravel

If migration is successfully created by using both commands then next step is to migrate

Command : php artisan migrate

Database Migration in Laravel

Now, a customers table is created with given fields define in the up method above. Migration file name is created like this:

(2018_11_16_094924_create_customers_table.php)

Database Migration in Laravel

Run Specific Migration :-

Php artisan Migrate commands run on migration directory which executes the high priority batch first then rest lower batch. If a particular migration is to be run then we have to create a temporary directory in the migration folder by using the mkdir command or manually. At last we can process the migrations by using path of the directory created above .

php artisan migrate –path=/database/migrations/temporaryfolder

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *