Multilingual E-commerce API using Laravel 8.0 – Part 2 Database Migrations

Now that we have the initial setup completed, we need to create migrations for the product and a category controllers.

Please note that we will also have language translations. So we will have add a translation table as well.

#Step 1

Open a terminal in your project root directory and type the following command to create a database migration for the products table.

php artisan make:migration create_products_table

This should create a new migration in your database/migrations directory

For now we are going to add the product_price and the category_id in this table as our price and category id will not be affected by the language-translation.

$table->integer('product_price');
$table->foreignId('category_id')->constrained();

The migration also needs a down() method just in case there is a need to revert the migrations.

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->integer('product_price');
            $table->foreignId('category_id')->constrained();
            $table->timestamps();
        });
    }

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

#Step 2

Next we need to add a migration for product translation. We will be translating only the product name and product description for now.

php artisan make:migration create_product_translation_table

This should create a new migration in your database/migrations directory

We will be adding the following fields in the product translation table

  • Product Name
  • Product Description
  • Product ID (This links back to the products table)
  • Language ID (This id links to the languages table)
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('product_translations', function (Blueprint $table) {
            $table->id();
            $table->string('product_name');
            $table->text('product_description');
            $table->foreignId('product_id')->constrained('products')->onDelete('cascade');
            $table->foreignId('language_id')->constrained('languages');
            $table->timestamps();
        });
    }

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

The onDelete(‘cascade’) method will delete the product translations if the product is deleted from the products table.

#Step 3

Create one more migration for the category table

php artisan make:migration create_categories_table

For now, we will not be creating a translation for the category table. We will come back to this later in the latter tutorials 🙂

Here’s the category migration:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('category_name');
            $table->text('category_description');
            $table->timestamps();
        });
    }

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

#Step 4

We also need a languages migration. Let’s create a new migration for this.

php artisan make:migration create_languages_migration

In the languages table we will be adding the following fields

  • Language name
  • Language code

Here’s the completed migration for the languages table

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('languages', function (Blueprint $table) {
            $table->id();
            $table->string('language_name');
            $table->text('language_code');
            $table->timestamps();
        });
    }

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

#Step 5

The final step is to run the migrations

php artisan migrate

Series Navigation<< Multilingual E-commerce API using Laravel 8.0 – SetupMultilingual E-commerce API using Laravel 8.0 – Part 3 Product Resource and Product Resource Collection >>