如何修复Laravel中Psalm提示的UndefinedMagicMethod错误?
修复Psalm检测到Laravel模型魔术方法不存在的问题
检测到的错误:
UndefinedMagicMethod Magic method App\Models\User::wherenull() does not exist UndefinedMagicMethod Magic method App\Models\User::create() UndefinedMagicMethod Magic method App\Models\User::get()
已安装的依赖包:
"vimeo/psalm": "^5.24""psalm/plugin-laravel": "^2.11""barryvdh/laravel-ide-helper": "^v3.0"
已执行的命令:
php artisan ide-helper:modelsphp artisan ide-helper:generate
修复步骤:
确认Psalm Laravel插件配置正确
检查项目根目录的psalm.xml(或psalm.xml.dist)文件,确保已添加Laravel插件配置:<plugins> <pluginClass class="Psalm\LaravelPlugin\Plugin" /> </plugins>重新生成带强制覆盖的模型IDE注释
之前的命令未强制覆盖模型文件,导致PHPDoc注释未正确生成。执行以下命令:php artisan ide-helper:models -W该命令会直接在User模型文件中添加完整的PHPDoc注释,包含Eloquent提供的魔术方法和查询构造器方法。
清除Psalm缓存
缓存可能残留旧检测结果,执行清除命令:./vendor/bin/psalm --clear-cache验证模型继承关系
确保App\Models\User类正确继承Eloquent基础模型:namespace App\Models; use Illuminate\Database\Eloquent\Model; class User extends Model { // 模型代码 }若使用自定义基础模型,需保证最终继承链指向
Illuminate\Database\Eloquent\Model。重新运行Psalm检测
执行检测命令验证问题是否解决:./vendor/bin/psalm
内容的提问来源于stack exchange,提问作者Goryyn
相关产品推荐
相关产品推荐

