Everything You Need To Know Laravel Migration Skip to main content

To Know Laravel Migration

Saurabh Dhariwal

Saurabh Dhariwal

Laravel Migration

Introduction

 

  • Laravel migrations provide mechanisms for creating and modifying database tables.
  • Migrations also allow you to roll back the most recent changes that you made to a database.

 

Generate Migrations

 

  • To create a migration, use the make:migration Artisan command:
  • When you create a migration file, Laravel stores it in /database/migrations directory.
  • Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations.
  • Open the command prompt or terminal depending on your operating system.

 

Create Migration Table

 

Run the following command to create a migration file: php artisan make:migration create_yourtablename_table

Here,

 

  • Php artisan make:migration executes the make migration method via the artisan command.
  • You will get following results for your migration file.

 

Create Migration: 2016_09_15_2434_create_yourtablename_table

 

Migration Structure

 

We will now examine the contents of the created migration file Open the file.

 

/database/migrations/2016_09_15_2434_create_yourtablename_table

Laravel Migration

Here,

  • Class CreateYourtablenameTable extends Migration defines the CreateYourtablenameTable class that extends Migration class.
  • Public function up() defines the function that is executed when the migration run.
  • Public function down() defines the function that is executed when the migration rollback.

 

How to create a table using Migration

 

  • Now that we have successfully created migration file for yourtablename.
  • We will add the table definition fields in the Migration file.
Laravel Migration

Here Explain Migration,

 

  • Schema::create('yourtablename', function (Blueprint $table) {...} calls the create function of the Schema class. The create function is responsible for creating the database table.
  • $table->increments('id'); increments are used to define an auto increment field.
  • $table->string('yourname',60); string is used to define varchar fields. The second parameter is the length of the field.
  • $table->text('youraddress'); is used to define text fields.
  • $table->integer('pincode'); integer is used to define int fields.
  • $table->date('birthdate'); is used to define date fields.
  • $table->timestamps(); is used to automatically create two time stamp fields namely created_at and updated_at.

 

Go back to the command prompt Or terminal Run the following command for run migration file.

 

php artisan migrate

 

You will get the following output

Laravel Migration

Laravel Migration Rollback

 

  • One of the most advantages of migrations is that they allow you to roll back to the previous state before you run the migrations.
  • In this section, we will roll back the creation of the tables.
  1. Go back to the command prompt
  2. Run the following command

 

php artisan migrate:rollback

 

You will get the following output.

Laravel Migration

Hope this would help you and make Laravel Migration easy going. Drop us the message if you have any queries/concern regarding Laravel.

 

We are readily available to resolve it. #letstalksolution

Frequently Asked Questions

What exactly is Laravel Migration and why should I care about it?

Laravel Migration is a feature that helps manage database changes in your web application. It's crucial for evolving your database schema over time without causing disruptions. Think of it as an organized way to upgrade and version control your database.

How does Laravel Migration simplify the database setup process?

Laravel Migration provides a structured way to define database schema changes using code instead of SQL. This makes sharing and reproducing the database setup easy across different environments, enhancing consistency and reducing errors.

Can I roll back changes if something goes wrong during a migration?

Absolutely! Laravel Migration allows you to roll back migrations, reverting your database to its previous state. This rollback feature is handy if an error occurs during the migration process.

What types of database changes can I make using Laravel Migration?

Laravel Migration supports various database changes, including creating and modifying tables, defining indexes, and altering columns. It's a versatile tool for managing your database structure.

How do I create a new migration file in Laravel?

Creating a migration file is simple. You can use the php artisan make: migration command, followed by a descriptive name, to generate a new migration file. This file contains the instructions for your database changes.

Can I use Laravel Migration with an existing database?

Yes, you can! Laravel Migration is designed to work with both new and existing databases. You can create migration files to represent the current state of your database and use them to manage future changes.

How can I run a specific migration in Laravel?

To run a specific migration, use the php artisan migrate --path command followed by the path to the directory containing the migration file. This allows you to execute a specific migration without running all pending migrations.

Can I seed my database with Laravel Migration?

Yes, you can seed your database with test data using Laravel Seeder in conjunction with Migration. Seeder allows you to populate tables with sample data, making it useful for testing and development.