Create migration that deletes budgets whose tags don't exist anymore
ADD
database/migrations/2021_06_23_200708_delete_budgets_without_tags.php
+<?php
+
+use App\Models\Budget;
+use App\Models\Tag;
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class DeleteBudgetsWithoutTags extends Migration
+{
+ public function up(): void
+ {
+ foreach (Budget::all() as $budget) {
+ $hasStrayTag = $budget->tag_id && !$budget->tag;
+
+ if ($hasStrayTag) {
+ $budget->delete();
+ }
+ }
+ }
+
+ public function down(): void
+ {
+ //
+ }
+}

Create migration that deletes budgets whose tags don't exist anymore