Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. <?php
  2.  
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5.  
  6. class ProductsRelationshipChanges extends Migration {
  7.  
  8. /**
  9. * Run the migrations.
  10. *
  11. * @return void
  12. */
  13. public function up()
  14. {
  15. // Firstly, add group_id to the products table
  16. Schema::table('products', function($t){
  17. $t->integer('group_id')->unsigned();
  18. });
  19.  
  20. // merge all of the group_id's from group_product to products
  21. $results = DB::table('group_product')->get();
  22.  
  23. // This should be each row in group_product?
  24. foreach($results as $result) {
  25.  
  26. // for the product id, look up that row in products
  27. DB::table('products')->where('id', '=', $result->product_id)->insert([
  28. 'group_id' => $result->group_id
  29. ]);
  30.  
  31. // Finally, drop the group_product pivot
  32. Schema::drop('group_product');
  33. }
  34.  
  35. /**
  36. * Reverse the migrations.
  37. *
  38. * @return void
  39. */
  40. public function down()
  41. {
  42. Schema::table('products', function($t){
  43. $t->dropColumn('group_id');
  44. });
  45.  
  46. // restore the group_product pivot
  47. Schema::create('group_product', function($t){
  48. $t->increments('id');
  49. $t->integer('group_id')->unsigned();
  50. $t->integer('product_id')->unsigned();
  51. $t->integer('position')->unsigned();
  52. $t->timestamps();
  53.  
  54. $t->foreign('group_id')->references('id')->on('groups')->onUpdate('cascade')->onDelete('cascade');
  55. $t->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
  56. $t->index(array('position', 'product_id'));
  57.  
  58. });
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement