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 the 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
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 runs.
- Public function down() defines the function that is executed when the migration is rolled back.
Hire Laravel Developers to create secure, robust, and bug-free web applications.
Pooja Upadhyay
Director Of People Operations & Client Relations
How to create a table using Migration
- Now that we have successfully created a migration file for your table name.
- We will add the table definition fields in the Migration file.
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 to run the migration file.
php artisan migrate
You will get the following output
Laravel Migration Rollback
- One of the biggest 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.
- Go back to the command prompt
- Run the following command
php artisan migrate: rollback
You will get the following output.
Hope this will help you and make Laravel Migration easygoing. Drop us a message if you have any queries/concerns regarding Laravel.
We are readily available to resolve it. #letstalksolution
Frequently Asked Questions
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.
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.
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.
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.
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.
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.
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.
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.
Let’s Start your Next Laravel Development Project.
Pooja Upadhyay
Director Of People Operations & Client Relations