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

执行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()函数检查数组偏移时触发非法类型错误。

修复步骤

  1. 参数校验前置:在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);
    }
    
  2. 排查调用源头:检查loop.blade.php中调用getDisplayNumberServiceInLocation的代码(当前提供的片段未显示,但错误关联到此处),确保传入的$service_type是合法的字符串/整数。可以参考视图中is_string($service_type)的校验逻辑,仅在参数合法时调用该方法。
  3. 验证服务列表结构:确认get_bookable_services()返回的数组结构,确保其键为字符串/整数类型,避免出现非标量键导致的偏移错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 15:16:11