In this article, we’ll explore the basics of Laravel migrations and how to use them effectively. Migrations are like version control for your database, allowing your team to define and share the application’s database schema definition.
Popular PHP web application framework Laravel offers a variety of features and tools for creating cutting-edge and reliable web apps.
Its built-in support for database migrations, which helps developers to manage database changes and updates in a logical and organized fashion, is one of Laravel’s core features.
In this post, we’ll examine Laravel migration in more detail and discuss how it can improve the way you handle database schema changes.
Laravel migration is a feature that allows developers to define and manage database schema changes in a structured and repeatable way. You can use Laravel migration to create, modify, or delete database tables, columns, indexes, and other schema elements using simple PHP code.
Before the go to the deep dive into Laravel migration, let’s understand some common terms of migrations:
The make:migration
Artisan command in Laravel can be used to build a new migration by producing a new migration file in the database/migrations directory. Two methods—up and down—that specify the actions to be taken when the migration is applied or rolled back, respectively, are included in the migration file.
Let’s create a users table using the migration command:
php artisan make:migration create_users_table
This will generate a new migration file with a name like 2023_04_09_000000_create_users_table.php
into database/migrations directory. You can then open the file and define the up and down methods like this:
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(); }); } public function down() { Schema::dropIfExists('users'); }
The up()
method creates a new users table with columns, while the down method drops the table if it exists.
You can then run the migration using the migrate Artisan command:
php artisan migrate
Once the run the above command, It’ll create the users table in your database.
Sometimes, You need to modify an existing migration. You can create a new migration that modifies the schema and runs it after the original migration. Let’s update our users table and add a new column here, The new migration like this:
php artisan make:migration add_role_to_users_table
A new migration file with the name 2023_04_09_000001_add_role_to_users_table.php
will be created as a result. The up and down methods can then be defined by opening the file as follows:
public function up() { Schema::table('users', function (Blueprint $table) { $table->string('role')->default('user'); }); } public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('role'); }); }
The up()
function adds a new role column with the default value of user to the users table, whereas the down()
method removes the column from the table.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More