What is migrations in Laravel:
Migrations in laravel allows all team member at any place to easily modify and share application database schema.
It is paired with Laravel schema builder to easily build database schema.
We can create tables ,add columns, alter them by creating migrations.
How to create migration using artisan command:
Step 1 : Run Composer.
Step 2 : Type Following command-
Migration looks like :
Migration has 2 methods :
1.up() : In this method we can add tables,columns,indexes and also do other operations with the table.
2.down() : This method reverses the operations performed by up() method.
Example to create migrations for articles table -
Articles table has 3columns and their datatypes are:
1.id - int,primary key
2.title - string
3.description-Text
To create new table using migration we use '--create=tablename ' option and if we want to work on existing table we use ' --table=tablename ' option
Step 1 :Run composer and add following code -
1.table name=articles
2.Second parameter is a
> Using $table object we can add columns ,like
$table->string('name') ,where string() is specifying datatype for column 'name'.
> timestamps() adds created_at and updated_at() field in database table.
Migration is ready to be migrated.
Step 3 : Run migration using following artisan command-
Since now article table is created we are going to create model for articles table so that we can insert delete and update new rows in articles table
Summary :
Migrations makes creating database and adding columns easiers ,just create migration for that table add the columns or indexes and just migrate ,thats it.
With migration any team member can share and modify the database schema as per the needs.
Migrations in laravel allows all team member at any place to easily modify and share application database schema.
It is paired with Laravel schema builder to easily build database schema.
We can create tables ,add columns, alter them by creating migrations.
How to create migration using artisan command:
Step 1 : Run Composer.
Step 2 : Type Following command-
" php artisan make:mmigration CreateUsersTable"Note :Migrations resides in Database/migration directory.
Migration looks like :
Migration has 2 methods :
1.up() : In this method we can add tables,columns,indexes and also do other operations with the table.
2.down() : This method reverses the operations performed by up() method.
Example to create migrations for articles table -
Articles table has 3columns and their datatypes are:
1.id - int,primary key
2.title - string
3.description-Text
To create new table using migration we use '--create=tablename ' option and if we want to work on existing table we use ' --table=tablename ' option
Step 1 :Run composer and add following code -
" php artisan make:migration Create_Article_Table --create=Articles "Step 2 : Code to add columns to the table
<?php> up() method consist of create() method which accepts two parameters :use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Create_Article_Table extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title ');
$table->text('description')
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('articles');
}
}
1.table name=articles
2.Second parameter is a
Closure
which receives a Blueprint
object used to define the new table.> Using $table object we can add columns ,like
$table->string('name') ,where string() is specifying datatype for column 'name'.
> timestamps() adds created_at and updated_at() field in database table.
Migration is ready to be migrated.
Step 3 : Run migration using following artisan command-
" php artisan migrate"Step 4 :Now you can check in database that articles table is created.
Since now article table is created we are going to create model for articles table so that we can insert delete and update new rows in articles table
Summary :
Migrations makes creating database and adding columns easiers ,just create migration for that table add the columns or indexes and just migrate ,thats it.
With migration any team member can share and modify the database schema as per the needs.