Here’s the Laravel Migration Guide You’ve Been Looking for!

By Web Development India| Date posted: | Last updated: March 8, 2022
Laravel Migration

Laravel migration may look like one complicated subject but worry not; this guide will help you understand it from the basics.

What is Laravel Migration?

Laravel Migration is a process that enables you to build a table in a database without generally writing or modifying the SQL queries. It allows you to roll back changes, make incremental changes or sync the database structure simultaneously when multiple teams work on the same application.

To simple understand, it is a quick method to create, destroy or bootstrap any application database. There originates no need to run a SQL query or log into the database console.

What is the point of Laravel Migration when you can directly create a table?

While working as a team, if one needs to execute some alteration in the table, pursuing the traditional method can be hectic.

While working traditionally, SQL query needs to be passed to the team member who will have the responsibility to import the file. In a situation, if they forget to do so, the application won’t work accurately.

To cater to such more problems, Laravel Migrations came as a saviour. It helps you get rid of any team collaboration issues in concern with databases.

By utilizing the Laravel migrations, you can add new columns, delete records, and more without deleting records in your database. Rest assured of the already existing migrations, as Laravel will only do migrations that are newly added based on your request. You do not have to worry about previous migrations because Laravel has a track on the migrations already executed in the database.

How to get started?

Let’s understand it with a simple task. We will perform a database migration where we will create a table to save the application links.

To do so, we will be utilizing the artisan tool that is available in artisan by default.

Step 1: We will start with the Application’s root directory. Make sure that your Docker composes development environment should be up and running.

Step 2: Next, to set up the links table, you need to create a database migration.

To create the same, run the make:migration Artisan command, which will bootstrap a new class in the database/migrations folder on your Laravel application. A default boilerplate code will be present in the class by default.

php artisan make:migration create_flights_table

Remember:

Don’t forget to use the docker-compose exec app. It is used to run the command where PHP is installed on the app service container.

The migration that is generated is based on the current date and time.

Even the name provided make:migration command will be an argument.

Step 3: utilizing the editor of choice, open the generated migration class.

Further, to include the table columns in order to store the table data, you need to update the up method.

Step 4: The next step includes the replacement of the current data. To do the same, the following codes needed to be worked on. Three new fields in the table will be added.

  • URL: It is required to save the link URL.
  • Description: To save the link description, this text field needs to be filled in.
  • Enabled: whether enabled or not, this field needs to be filled in to store the state of the link. A value of 0 or 1 will be stored to the tinyint unsigned field by the boolean Schema type.
<AMP-analytics type = “googleanalytics” id = “analytics1”>
  <?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

Step 5: Save the migration file after all these entries.

Step 6: Next step includes running the migration file.

Use the code, “docker-compose exec app PHP artisan migrate.”

You can also get a glance at the other migrations that were executed in the same table in the database/migrations in the app’s root folder.

It is the most basic procedure on how you can create a table using migrations. Hope you will be able to do a much better job with this step-by-step guide.

Furthermore, to help you learn more about the Laravel migration, we have listed some Laravel commands that will prove to be extremely useful to you.

  • migrate:fresh
    Using this command, one can drop all the tables from the database and re-run all the migrations.

    Syntax used: php artisan migrate:fresh

  • migrate:install
    Using this command, a migration table can be created in a database.

    Syntax used: php artisan migrate:install

  • migrate:refresh
    Using this command, all the migrations can be rolled back, and migrations can be re-run.
    In simple words, the database can be re-created entirely.

    Syntax used: php artisan migrate:refresh

  • migrate:reset
    It solely drops all the tables that you have created in a database,

    Syntax used: php artisan migrate:reset

  • migrate:rollback
    This laravel command is used to drop the last database migration.

    Syntax used: php artisan migrate: rollback

  • migrate:status
    This command helps you check the migration status.

    Syntax used: php artisan migrate: status

Get started!
Laravel will ease the burden off your shoulders with its great framework. You can bid goodbye to an array of mundane tasks and can take advantage of several other features using laravel that will get your work done within seconds.

So, there stands no reason why you should not be using laravel. Be confident as you take the help of this step-by-step guide and start your laravel migration journey today!

We provide all-in-one Laravel Development services starting from Website, Web Application, Web Services, Backend, RESTful API, ERP (Enterprise Resource Planning), CRM (Customer Relationship Management), SaaS (Software as a Service) and Cloud-based software based on your business needs. Skynet Technologies has expertise in developing feature-rich, robust, and customized Laravel website and web application based on your business requirements. We offer full suite of Laravel Maintenance Services starting from website content updates, website speed optimization, SSL installation, Source Code Audit, Security Patches Updates, Bug Fixing and Troubleshooting, Broken Link Resolution, Third Party Integration, Migration or Version Upgrades.

If you have any questions or would like to know more about how Skynet Technologies can help your business to reach one step ahead, Reach out us through submit form & We'll get back to you soon!

Request for Quote

What is five plus 2?

Answer a question to submit a form.