diff --git a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php index 5b7cbbd76cc9..5249769d4557 100644 --- a/src/Illuminate/Foundation/Testing/DatabaseTruncation.php +++ b/src/Illuminate/Foundation/Testing/DatabaseTruncation.php @@ -85,12 +85,34 @@ protected function truncateTablesForConnection(ConnectionInterface $connection, fn ($tables) => $tables->intersect($this->tablesToTruncate), fn ($tables) => $tables->diff($this->exceptTables($name)) ) - ->filter(fn ($table) => $connection->table($table)->exists()) - ->each(fn ($table) => $connection->table($table)->truncate()); + ->filter(fn ($table) => $connection->table($this->removeTablePrefix($connection, $table))->exists()) + ->each(fn ($table) => $connection->table($this->removeTablePrefix($connection, $table))->truncate()); $connection->setEventDispatcher($dispatcher); } + /** + * Remove the table prefix from a table name, if it exists. + * + * @param \Illuminate\Database\ConnectionInterface $connection + * @param string $table + * @return string + */ + protected function removeTablePrefix(ConnectionInterface $connection, string $table): string + { + // Retrieve the table prefix associated with the connection + $prefix = $connection->getTablePrefix(); + + // Check if the table name contains the prefix + if (strpos($table, $prefix) === 0) { + // If the prefix is found, remove it from the table name + return substr($table, strlen($prefix)); + } + + // If no prefix is found, return the original table name + return $table; + } + /** * The database connections that should have their tables truncated. *