执行PHP artisan migrate后出现Illegal offset type错误求助
问题:Illegal offset type in isset or empty 错误排查
错误详情
触发 ErrorException: Illegal offset type in isset or empty,错误关联文件及行号:
- 逻辑代码:
Location.php第62行 - 视图文件:
modules\Location\Views\frontend\blocks\list-locations\loop.blade.php第7行
相关代码片段
Location.php 对应方法
public function getDisplayNumberServiceInLocation($service_type) { $allServices = get_bookable_services(); if (empty($allServices[$service_type])) return false; $module = new $allServices[$service_type]; return $module->getNumberServiceInLocation($this); }
loop.blade.php 对应代码段
$translation = $row->translateOrOrigin(app()->getLocale()); $link_location = false; if (is_string($service_type)) { $link_location = $row->getLinkForPageSearch($service_type); }
问题原因与修复方案
核心原因
$allServices[$service_type] 中的 $service_type 并非合法的数组键类型(比如传入了对象、数组而非字符串/整数),导致empty()函数检查数组偏移时触发非法类型错误。
修复步骤
- 参数校验前置:在
getDisplayNumberServiceInLocation方法开头增加类型校验,过滤非法参数:public function getDisplayNumberServiceInLocation($service_type) { // 仅允许字符串或整数类型的键 if (!is_scalar($service_type)) { return false; } $allServices = get_bookable_services(); if (empty($allServices[$service_type])) return false; $module = new $allServices[$service_type]; return $module->getNumberServiceInLocation($this); } - 排查调用源头:检查
loop.blade.php中调用getDisplayNumberServiceInLocation的代码(当前提供的片段未显示,但错误关联到此处),确保传入的$service_type是合法的字符串/整数。可以参考视图中is_string($service_type)的校验逻辑,仅在参数合法时调用该方法。 - 验证服务列表结构:确认
get_bookable_services()返回的数组结构,确保其键为字符串/整数类型,避免出现非标量键导致的偏移错误。
内容的提问来源于stack exchange,提问作者Niklas
相关产品推荐
相关产品推荐

