The Benefits of Using Migrations in Laravel
Tags: laravel
Published: 2023-03-13 15:45:50

---
title: The Benefits of Using Migrations in Laravel
published: true
date: 2023-02-01 17:28:35 UTC
tags: Laravel,laravel
canonical_url: https://larachamp.com/the-benefits-of-using-migrations-in-laravel/
---

Migrations in laravel, It’s a kind of fundamental topic to discuss but some of the most important things that I wanted to discuss is the Laravel Migration Feature.

We know that we can create tables via PHP or also manually. But this is for newcomers who just don’t look at this cool feature that makes ready a table in seconds for you. This is what that I wanted to say that if you are doing job somewhere and not using Laravel migration when building application then you are the too unproductive person in this AI phase.

**Use Migrations when creating tables or updating anything in the table. Be a productive laraveler.**

## Why I’m Saying This

It’s been more than one year since I’ve been working with Laravel. So, in that one year by watching [**Laravel daily**](https://laraveldaily.com/)I learned a lot about laravel. Now I at least have good knowledge about building applications in Laravel. But what if you are working with some team members who just don’t care about this thing? They just don’t think about the flow. Who will be answerable? Everyone.

![](https://larachamp.com/wp-content/uploads/2023/02/download.jpg)
_laravel migrations to create the table_

And one more thing is that if we don’t have enough time to do this, then listen “**[Migrations](https://laravel.com/docs/9.x/migrations)don’t extra time, only creating tables manually takes more than writing a migration file”.**

## Advantages Of Migration

See the above, it’s my story. So, I want you to guys leave this thing if you are doing it. This is just a personal opinion that you should see the future.

1. If some other developer will be working on your script by taking a pull, You don’t have to send him or her the SQL files to import the db.
2. It gives efficient results when we work in a team.
3. One of the most important things that I love about migration is that it has very simple methods (anyone can understand easily) to handle table creation and modification.
4. It’s kind of flexible anytime you can modify your table just by writing a few lines. Like, I want to update the column name then I don’t have to do go to the manual or change the name. No! Just do the below thing.

```
php artisan make:migration update_media_table_column_name --table=media //example

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('media', function (Blueprint $table) {
            $table->renameColumn('old_column_name', 'new_column_name');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('media', function (Blueprint $table) {
            $table->renameColumn('new_column_name', 'old_column_name');
        });
    }
};

```

So, as we did above you can do this for any reason. May be you want to add missing column to the table. You can then add the column. So, all over it’s kind of cool thing if you will read about this. I’m only talking about the migration because I wasted half of the day because of this, and some tasks are pending because of this only. A good team needs a good understanding so cooperate each other by following the standards.

## Creating migration for a table

Let’s take an example, we want to create table to store videos data on database. Then first of all configure env file for database connection. And write command as below:

```
php artisan make:migration create_videos_table
```

This will create table for you. After this you can go to the created migration file and add the columns and properties.

```
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('videos', function (Blueprint $table) {
            $table->id();
            $table->string('name')->default('untitled');
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('video_url')->nullable();
            $table->string('thumbnail_url')->nullable();
            $table->string('mime_type')->nullable();
            $table->string('duration')->nullable();
            $table->string('size')->nullable();
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('videos');
    }
};

```

We use string() for Varchar and Integer() for Int. the maximum time we use these two properties. And in the end, we have passed timestamps() this method automatically creates the ‘created\_at’ and ‘updated\_at’ columns.

## Conclusion

So, In short, it’s very helpful if you will follow this then you team person will not be disappointed because of the process. And when you do work in a company you are answerable to someone else. Then if you will take extra time because of someone then you can’t blame anybody. So, we should give our best in our dev work so that everyone can work with a good workflow. And this was what I’m feeling today about migrations. Thanks for reading.

**[Optimize Your Laravel Application](https://dev.to/_gurpreetkumar/7-tips-for-optimizing-your-laravel-web-application-1mhh)**

The post [The Benefits of Using Migrations in Laravel](https://larachamp.com/the-benefits-of-using-migrations-in-laravel/) appeared first on [Larachamp](https://larachamp.com).