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 Migr...
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 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.
And one more thing is that if we don't have enough time to do this, then listen "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.
- 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.
- It gives efficient results when we work in a team.
- 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.
- 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.