Laravel Lumen 5.4执行迁移时抛出‘non-numeric value encountered’错误
解决Laravel Lumen 5.4数据库迁移的"A non-numeric value encountered"错误
嘿,我之前也踩过这个坑!这个错误一般是PHP 7.1+版本才会触发的,因为Lumen 5.4的迁移代码在处理批次号的时候,没做严格的类型转换,PHP 7.1开始对这种非数值转数值的操作会直接抛错,刚好就炸在DatabaseMigrationRepository.php第113行那了。下面给你几个靠谱的解决办法:
临时救急:直接修改Vendor文件
如果你赶时间,先直接改vendor里的文件凑合用:
- 找到项目里的
vendor/illuminate/database/Migrations/DatabaseMigrationRepository.php - 找到第113行(大概是
return $this->getNextBatchNumber() + 1;),把它改成:
return (int)$this->getNextBatchNumber() + 1;
不过要注意,下次composer update的时候这个修改会被覆盖,所以最好用下面的持久化方案。
持久化方案:用Composer补丁
直接改vendor不是长久之计,用补丁工具可以让修改跟着项目走:
- 先安装补丁工具:
composer require cweagans/composer-patches --dev
- 在项目根目录建个
patches文件夹,里面新建database-migration-fix.patch文件,内容如下:
diff --git a/Migrations/DatabaseMigrationRepository.php b/Migrations/DatabaseMigrationRepository.php index 8a9f7d6..d2b3c1e 100644 --- a/Migrations/DatabaseMigrationRepository.php +++ b/Migrations/DatabaseMigrationRepository.php @@ -110,7 +110,7 @@ class DatabaseMigrationRepository implements MigrationRepositoryInterface */ public function getNextBatchNumber() { - return $this->getLastBatchNumber() + 1; + return (int)$this->getLastBatchNumber() + 1; }
- 打开
composer.json,在extra节点里加补丁配置:
"extra": { "patches": { "illuminate/database": { "Fix non-numeric batch number error in Lumen 5.4": "patches/database-migration-fix.patch" } } }
- 最后执行
composer install,补丁就会自动应用啦。
排查数据库表问题
还有一种可能是你的migrations表出问题了,比如batch字段里有非数值的数据。这时候可以去数据库里检查migrations表,把batch列的所有值改成整数,或者备份数据后清空表重新迁移。
不推荐的临时方案:降级PHP
要是上面的方法都不想试,也可以把PHP版本降到7.0,因为PHP 7.0对这种隐式类型转换不会报错,但这只是权宜之计,还是建议用前面的方法从根源解决。
内容的提问来源于stack exchange,提问作者mike
相关产品推荐
相关产品推荐

