Laravel Migrations Simply Explained

Laravel Migrations plays a huge role in when managing large systems with complicated database schema with lots of tables. Even if your system is a small one, Laravel Migrations makes it lot easier for you to manage the Database.

What is Laravel Migrations

Simply, Laravel Migrations is a way of Managing (Creating, Updating, Deleting) database tables without having to use a Database Manager such as PHPMyAdmin or Command Line Interface.
 

Hope, ya’ll know what PHPMyAdmin is though. How did you create Database and all its tables before ?  You go to MySQL Command Line and hit some MySQL commands to create, update or delete tables. Each time you need to do this for making changes in your database. Or you have to use a Software such as PHPMyAdmin. That’s what you’ve been doing so far right?

But, when you’re using Laravel Migrations, you don’t create tables. You define tables. Then Laravel Migrations creates them for you. This applies to Create, Update or Delete tables or Columns.

Imagine a situation that you’re in the middle of production and suddenly new requirement comes up and it needs to rename a column of a table. What would you do ? It’s a struggle right ?

With Laravel Migrations, you just update the particular migration file and run a special command and that’s it.  That’s why Laravel Migrations are so important. It’s so much time and effort saving.

What are they look like ?

Open up Database folder in your installation Laravel root. Inside, you’ll see a folder named migrations. Open it up and there you’ll see Laravel Migration files that comes pre-included when installing Laravel.

Open one of them and check it out.

Laravel Migrations Folder

Give it a try

Let’s give it a try by installing Laravel.

OK. Now I need to create a table name Products to store all my products. Let’s create that with a Laravel Migration.

Open up the terminal and run the following command.

php artisan make:migration create_products_table

These are Artisan commands. You can see all of them in Laravel Documentation.

Now open up the migrations folder and you’ll see a new migration has been created.

Now I want my products table to have following columns.

  • id – Integer (Primary Key)
  • name – String
  • Description – Text
  • price – float

Now let’s define that table in our Laravel Migration. There are two written functions in a Laravel Migration file.

 
public function down() { 

}

And…

public function down() {

}

up() is for running the migration. down() is for reversing the migration. Which is the opposite of the action done by migration. For example, if you create a table, the opposite would be dropping the table. You define that too. That’s pretty helpful for managing the database.

This is how you define your table in the Laravel Migration file.


public function up(){
   Schema::create('products', function (Blueprint $table) {
      $table->increments('id');
      $table->string('name',100);
      $table->text('description')->nullable();
      $table->float('price');
   });
}

All the lines are self descriptive I guess. Only thing you’re questioning may be nullable() method. That’s for allowing empty values for that columns. That’s how you create a table with Laravel Migration.

As we’re supposed to, we should create the reverse action for this as well. For creating the table, the reverse action is dropping the table. We write that in down() method.

public function down()
{
    Schema::dropIfExists('products');
}

 

Running the Laravel Migrations

Now, your first Laravel Migration is complete. Still it hasn’t created any table. We need to run it. Type the following command to run your Laravel Migration. This will run all the new migrations.

php artisan migrate

running migrations laravel

Alright, now check your database and you’ll see products table has been created.

Like that, you can create, update, delete and do all the database management actions with Laravel Migrations. Please follow Laravel Documentation to chekout other migration methods and keywords.

Thanks!

Leave a Reply

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