Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- class ProductsRelationshipChanges extends Migration {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- // Firstly, add group_id to the products table
- Schema::table('products', function($t){
- $t->integer('group_id')->unsigned();
- });
- // merge all of the group_id's from group_product to products
- $results = DB::table('group_product')->get();
- // This should be each row in group_product?
- foreach($results as $result) {
- // for the product id, look up that row in products
- DB::table('products')->where('id', '=', $result->product_id)->insert([
- 'group_id' => $result->group_id
- ]);
- // Finally, drop the group_product pivot
- Schema::drop('group_product');
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::table('products', function($t){
- $t->dropColumn('group_id');
- });
- // restore the group_product pivot
- Schema::create('group_product', function($t){
- $t->increments('id');
- $t->integer('group_id')->unsigned();
- $t->integer('product_id')->unsigned();
- $t->integer('position')->unsigned();
- $t->timestamps();
- $t->foreign('group_id')->references('id')->on('groups')->onUpdate('cascade')->onDelete('cascade');
- $t->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
- $t->index(array('position', 'product_id'));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement