PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, November 16, 2022

[FIXED] How to clone Laravel migration fields into another migration?

 November 16, 2022     laravel, laravel-6, laravel-migrations     No comments   

Issue

I have this Laravel migration structure:

class CreateWarehouseProductTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('warehouse_products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id')->default(0);
            $table->integer('warehouse_id');
            $table->integer('free_amount')->default(0);
            $table->integer('booked_amount')->default(0);
            // ...
            $table->timestamps();
        });
    }

    // ...
}

I need to create a daily backup from warehouse products and for this I need to create a new table what is exactly same as warehouse_products and contains one more extra field for the backup's date.

Is there any best practice to do this? I think something like this:

class CreateWarehouseProductBackupTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('warehouse_product_backups', function (Blueprint $table) {
            CreateWarehouseProductTable->cloneFieldsToHere();
            $table->date('date_of_backup');
        });
    }
}

Is there something similar good practice to clone fields from an existing migration?


Solution

I found a solution, I think not elegant, but it's working:

class CreateWarehouseProductTable extends Migration
{
    public function up()
    {
        Schema::create('warehouse_products', function (Blueprint $table) {
            $this->setWarehouseProductColumns($table);
        });

        Schema::create('warehouse_product_backups', function (Blueprint $table) {
            $this->setWarehouseProductColumns($table);
            $table->date('date_of_backup')->nullable();
        });
    }

    public function setWarehouseProductColumns(Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id')->default(0);
            $table->integer('warehouse_id');
            $table->integer('free_amount')->default(0);
            $table->integer('booked_amount')->default(0);
            // ...
            $table->timestamps();
    }
}


Answered By - netdjw
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing