You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目执行迁移/创建Model时报错求助(含TypeError等)

长期未维护的Laravel项目执行迁移/创建Model时的错误排查与解决

核心错误分析

1. Carbon\Carbon类型不匹配错误

TypeError 

  Carbon\Carbon::setLastErrors(): Argument #1 ($lastErrors) must be of type array, bool given, called in C:\xampp\htdocs\projetsoutenance\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php on line 98

  at C:\xampp\htdocs\projetsoutenance\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php:928
    924▕      * @param array $lastErrors
    925▕      *
    926▕      * @return void
    927▕      */
  ➜ 928▕     private static function setLastErrors(array $lastErrors)
    929▕     {
    930▕         static::$lastErrors = $lastErrors;
    931▕     }
    932▕

原因:旧版本Carbon依赖date_get_last_errors()的返回值类型,但PHP 8.0+中该函数无错误时返回bool(false),而旧版Carbon的setLastErrors方法强制要求参数为array,类型不匹配触发报错。


2. Ignition包字符串插值语法弃用错误

Whoops\Exception\ErrorException 

  Using ${var} in strings is deprecated, use {$var} instead

  at C:\xampp\htdocs\projetsoutenance\vendor\facade\ignition\src\SolutionProviders\MergeConflictSolutionProvider.php:52
     48▕     }
     49▕
     50▕     protected function getCurrentBranch(string $directory): string
     51▕     {
  ➜  52▕         $branch = "'" . trim(shell_exec("cd ${directory}; git branch | grep \\* | cut -d ' ' -f2")) . "'";
     53▕
     54▕         if ($branch === "''") {
     55▕             $branch = 'current branch';
     56▕         }

原因:PHP 8.0+弃用了${var}字符串变量插值语法,要求改为{$var},但项目中旧版本的facade/ignition包仍在使用弃用语法,触发错误。


解决步骤

方案一:升级兼容依赖包

  1. 先备份项目根目录的composer.json文件
  2. 更新Carbon到支持PHP 8+的版本:
    composer require nesbot/carbon:^2.62
    
  3. 更新Ignition到修复语法问题的版本:
    composer require facade/ignition:^2.17
    
  4. 若单独更新出现依赖冲突,可尝试全局更新兼容依赖(注意提前测试):
    composer update
    

方案二:降级PHP版本

如果项目无法升级依赖,可切换到项目原生支持的PHP版本:

  • Laravel 5.x对应PHP 7.0-7.4,Laravel 6.x对应PHP 7.3-7.4,Laravel 7.x对应PHP 7.2.5+,根据项目实际Laravel版本调整PHP环境。

内容的提问来源于stack exchange,提问作者Martial Denver

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 20:00:16