ownedByAnotherRow($table, $column, $slug, $legacyId)) { $slug = "{$base}-" . ++$suffix; } return $slug; } public function adoptableId(string $table, string $column, string $candidate): ?int { $slug = Str::slug($candidate) ?: 'item'; return DB::table($table)->where($column, $slug)->whereNull('legacy_id')->value('id'); } private function ownedByAnotherRow(string $table, string $column, string $slug, int $legacyId): bool { return DB::table($table) ->where($column, $slug) ->where(function ($query) use ($legacyId) { $query->whereNull('legacy_id')->orWhere('legacy_id', '!=', $legacyId); }) ->exists(); } }