As we know, PHP is the most popular scripting language for Web development. There are different frameworks based on PHP in the market. Laravel is one of the open-source PHP frameworks. It uses an MVC structure to develop the application using PHP and provides many functionalities for easier development of web applications.
In this blog, we will learn about Laravel Migration which is used to create tables in your database and allows you to share and modify the database schema of your application.
Laravel Migration serves as a collection of directives that outline the modifications intended for your database schema. These modifications encompass various tasks such as creating new tables, altering existing ones, adjusting columns, and initializing the database with essential data. Through the organization of these modifications into migration files, Laravel guarantees the synchronization of your database schema with your application's codebase. This synchronization facilitates the management of database alterations across diverse development environments and deployment phases. With Laravel, you can execute migrations without concerning yourself with the specifics of the underlying database system, whether it's MySQL, PostgreSQL, SQLite, or any other supported by Laravel. The framework abstracts the database-specific syntax, ensuring that migrations remain portable and adaptable to different database systems.
Why do we need Laravel Migration?
Let's say you are working in a team, and there is an instant need for modification in the database. In this case, you need to share the whole database file with another member who will modify it, which may require some installation and configuration. To avoid the tension of such configuration and installation, we use the Laravel Migration, which helps the programmer modify the database without deleting the existing records.
Let’s learn about configuration and environment files in laravel migration.
Laravel Migration - Environment Configuration
When we set up our project using composer, the two files .env and .env.example are created. The files are present in the folder /xampp/htdocs/project_name.
In our case, it is present in /xampp/htdocs/codingninjas.
The above image shows the two files .env and .env.example in the codingninjas directory.
Let’s understand the importance of these two files.
.env file: This file contains all the sensitive information like password, id, and other credentials in the key-value pair. This file will not be pushed in the git repository and only be shared with the programmers working on the project development.
.env.example: This file will contain all the key variables needed in the project and present in the .env file. This file will be pushed to the git repository so anyone can download and use the project using their own value variables.
Let’s see what is in the database.php file. This file is available in the /xampp/htdocs/project_name/config/ folder.
In our case, it is /xampp/htdocs/codingninjas/config/.
database.php file
In the above image, we can see that the database.php file returns an Array. The array is connection[ ] array that returns connections of different databases like MySQL, PgSQL, SQLSrv, etc. The values are assigned to variables using the env(key, value) function.
Understanding the Migration Methods
First, we need to understand the migration structure to understand the migration methods.
Let’s see the migration structure.
Step 1: Go to the \xampp\htdocs\project_name\database\migrations folder, in our case, \xampp\htdocs\codingninjas\database\migrations. You will see the files as shown in the screenshot below:
Step 2: Open any of the files. Let's say we open the 2014_10_12_000000_create_users_table.php file. The file contains the following code:-
<?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('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
The screenshot of the same is given below:
In the above code, we can see that the Migration class contains two methods, up( ) and down( ).
Now we are familiar with the migration structure. Let’s understand what is the role of the two methods, up( ) and down( ).
up( ) method: This method is used to create new tables, insert indexes, and add new columns.
down( ) method: This method is used to drop the fields. Its functionality is just opposite to the up( ) method.
How To Use Migrations to Create and Manage Database Tables in Laravel
Using migrations in Laravel to create and manage database tables involves several steps:
Creating Migrations: Run the php artisan make:migration create_your_table_name_table command. This creates a migration file in the database/migrations directory.
Defining Table Structure: Edit the generated migration file. Use the Schema builder to define the table columns and indexes. Laravel provides a fluent interface to specify column types, keys, and constraints.
Running Migrations: Execute php artisan migrate to apply the migrations. This command creates the tables in the database as defined in the migration files.
Rolling Back: To undo migrations, use php artisan migrate:rollback. This reverts the last batch of migrations.
Modifying Tables: To modify existing tables, create a new migration using the make:migration command and specify the changes in the migration file. Then, run php artisan migrate.
Seeding Data: Optionally, use seeders to populate tables with initial data after running migrations.
Frequently Asked Questions
How to perform migration in Laravel?
To perform migration in Laravel, first we need to create a migration file using make:migration command, and then define table structure, and run migrate command to execute the migrations and create the table in the database.
What is the benefit of Laravel migration?
Laravel Migration provides many functionalities for easier development of web applications. It helps the programmer to modify the database without deleting the existing records. Laravel Migration also removes the effort of installation and configuration thus making the tasks of developers more efficient.
Why Laravel is better than Python?
Laravel and Python are different technologies used for different purposes. Laravel is a secure PHP framework used in web development, while Python is a programming language having many applications. The choice between them depends on the project requirements and the developer's familiarity with the language.
What are 2 pros and cons of using Laravel framework?
Laravel's pros are that it helps in faster and efficient development of web applications and has detailed documentation. However, learning Laravel may be difficult for beginners, and its dynamic nature can sometimes decrease the performance.
What is the difference between migrate and migrate fresh in Laravel?
In Laravel, migrate applies new database migrations while preserving existing data. migrate:fresh drops all tables, effectively clearing the database, and then applies all migrations from scratch, providing a clean state without any existing data or table structures.
Conclusion
In this article, we have extensively discussed Laravel Migration, why we need migration and its configuration. I hope you enjoyed this blog on Laravel Migration.