Create Laravel Migration table into a Database
Limited Time Offer!
For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!
1. What are Migrations and Migration table in Laravel?
Laravel Migration is an essential feature in Laravel that allows you to “easily create a table in your database“. It allows you to create and modify the application’s database schema. You can modify the table by adding a new column or deleting an existing column. Migrations are like version control for your database. Migration tables are files that are created by an administrator.
2. Why do we use migrations?
Another common reason for migration is to move from an outdated system or legacy systems to a system that is designed for modern data needs. In the age of big data, new storage techniques are a necessity. For example, a company might choose to move from a legacy SQL database to a data lake or another flexible system.
3. What are the two essential method and it’s use?
There are two methods, first is up() method and second is down() method. In up() method we create an schema like column of create_table. while in down() method, we rollback a current migration table. in other way we can say both method are opposite to each other. One create a table and second drop the table. Take a look both methods operation:
public function up() //create a schema of a table
{
Schema::create('table_name', function (Blueprint $table) {
$table->id();
.
.
$table->timestamps();
});
}
public function down() //use for the rollback the table.
{
Schema::dropIfExists('table_name');
}
3. How to create Migrations table and also migrate table?
- Firstly we create a migration table into a database by just entering a command :
php artisan make: migration create_yourtable
- Second time, we create a schema into up() of the table which comes from migration.
Schema::create('yourtable', function (Blueprint $table) {
$table->id(); //it's autogenerated
$table->String('name)
$table->timestamps();
});
- At the last, we just migrate the migration table by:
php artisan migrate
- we can migrate a single table by a command into terminal:
php artisan migrate --path=/database/migrations/2020_04_01_064006_create_posts_table.php
- We can also check the status of current migration table by:
php artisan migrate: status
- Remember that, we can rollback the current migration table by simple command:
php artisan migrate: rollback
Before migration we shouldn’t forget to customise our database name from .env that’s it.