$ids * @return array occupied id => id */ public static function occupiedIds(string $table, array $ids): array { $ids = array_values(array_unique(array_filter( array_map('intval', $ids), fn (int $id) => $id > 0 ))); if ($ids === []) { return []; } return DB::table($table) ->whereIn('id', $ids) ->pluck('id') ->map(fn ($id) => (int) $id) ->flip() ->all(); } public static function bumpAutoIncrement(string $table): void { $driver = DB::connection()->getDriverName(); if (! in_array($driver, ['mysql', 'mariadb'], true)) { return; } // DDL implicit-commits MySQL, which would leak rows from PHPUnit DatabaseTransactions. if (DB::connection()->transactionLevel() > 0) { return; } $max = (int) DB::table($table)->max('id'); $next = max(1, $max + 1); $prefix = DB::connection()->getTablePrefix(); $safe = str_replace(['`', '.', ' '], '', $table); DB::statement('ALTER TABLE `'.$prefix.$safe.'` AUTO_INCREMENT = '.$next); } }