If we manage our database manually like if you need to add a column, You directly alter the MySQL database table and add the column.
If you need to add a new table into you MySQL database you directly create a new table.
But here is the question, If you are a number of developers and working on same project how will you get to know that who changed what in your database ?
If right now you are working on second or third version of app, How will you know what was your schema when you launched first version of your application?
The answer of all these question is Migration. Laravel provides a beautiful feature of database migration.
We can manage database versioning of application. we also can track who modified what? We can manage our database through server side code.
We can create migration class for a table by artisan command like:
1
|
php artisan make:migration create_products_table --create=products
|
Here we are creating migration class for products table. Above command will create a new class in database/migrations/sometimestamp_create_products_table.php like below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
|
Here are 2 functions in this class up() function is used to create the schema and down function is used to drop the schema if exist.
you can edit this according to your need. For example if you wanted to to string type column and you wanted to make id as unsigned you can do it like:
1
2
3
4
5
6
|
Schema::create('products', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('productName');
$table->string('brand');
$table->timestamps();
});
|
Now if you will run the below migration command it insert your class into the migration table and then will create a new table 'products'.
1
|
php artisan migrate
|
If you have done any mistake Laravel also gives feature to rollback your migration like:
1
|
php artisan migrate:rollback
|
This command rolls back the last "batch" of migrations.
You also can Rollback & Migrate In Single Command like:
1
|
php artisan migrate:refresh
|
The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database.
You also can define Foreign Key Constraints in migration class like if you have posts table and you wanted to foreign key constraint with users table column user_id, I mean who has created the post. You can do it like below:
1
2
3
4
5
|
Schema::table('posts', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
|