|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Database\Migrations\Migration; |
| 4 | +use Illuminate\Database\Schema\Blueprint; |
| 5 | +use Illuminate\Support\Facades\Schema; |
| 6 | + |
| 7 | +return new class extends Migration |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Run the migrations. |
| 11 | + */ |
| 12 | + public function up(): void |
| 13 | + { |
| 14 | + // Add cascade deletes to posts table |
| 15 | + Schema::table('posts', function (Blueprint $table) { |
| 16 | + $table->dropForeign(['user_id']); |
| 17 | + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
| 18 | + }); |
| 19 | + |
| 20 | + // Add cascade deletes to likes table |
| 21 | + Schema::table('likes', function (Blueprint $table) { |
| 22 | + $table->dropForeign(['post_id']); |
| 23 | + $table->dropForeign(['user_id']); |
| 24 | + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); |
| 25 | + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
| 26 | + }); |
| 27 | + |
| 28 | + // Add cascade deletes to comments table |
| 29 | + Schema::table('comments', function (Blueprint $table) { |
| 30 | + $table->dropForeign(['post_id']); |
| 31 | + $table->dropForeign(['user_id']); |
| 32 | + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); |
| 33 | + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); |
| 34 | + }); |
| 35 | + |
| 36 | + // Add cascade deletes to followers table |
| 37 | + Schema::table('followers', function (Blueprint $table) { |
| 38 | + $table->dropForeign(['following_id']); |
| 39 | + $table->dropForeign(['follower_id']); |
| 40 | + $table->foreign('following_id')->references('id')->on('users')->onDelete('cascade'); |
| 41 | + $table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade'); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Reverse the migrations. |
| 47 | + */ |
| 48 | + public function down(): void |
| 49 | + { |
| 50 | + // Restore original foreign keys without cascade |
| 51 | + Schema::table('posts', function (Blueprint $table) { |
| 52 | + $table->dropForeign(['user_id']); |
| 53 | + $table->foreign('user_id')->references('id')->on('users'); |
| 54 | + }); |
| 55 | + |
| 56 | + Schema::table('likes', function (Blueprint $table) { |
| 57 | + $table->dropForeign(['post_id']); |
| 58 | + $table->dropForeign(['user_id']); |
| 59 | + $table->foreign('post_id')->references('id')->on('posts'); |
| 60 | + $table->foreign('user_id')->references('id')->on('users'); |
| 61 | + }); |
| 62 | + |
| 63 | + Schema::table('comments', function (Blueprint $table) { |
| 64 | + $table->dropForeign(['post_id']); |
| 65 | + $table->dropForeign(['user_id']); |
| 66 | + $table->foreign('post_id')->references('id')->on('posts'); |
| 67 | + $table->foreign('user_id')->references('id')->on('users'); |
| 68 | + }); |
| 69 | + |
| 70 | + Schema::table('followers', function (Blueprint $table) { |
| 71 | + $table->dropForeign(['following_id']); |
| 72 | + $table->dropForeign(['follower_id']); |
| 73 | + $table->foreign('following_id')->references('id')->on('users'); |
| 74 | + $table->foreign('follower_id')->references('id')->on('users'); |
| 75 | + }); |
| 76 | + } |
| 77 | +}; |
0 commit comments