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

Laravel报错Call to undefined method Closure::getRelationExistenceQuery()求助

问题原因

whereHas 方法的第一个参数必须是关联关系的名称(字符串),你直接传入了闭包,导致Laravel将该闭包当作关联关系对象去调用getRelationExistenceQuery()方法,从而触发了“未定义方法”的错误。

解决方法

分两种场景处理:

场景1:直接查询当前表的字段

如果只是要筛选Investigations表中name包含consultation的记录,不需要用whereHas,直接使用普通where条件即可:

public function payments(){
    $data = Investigations::where('name', 'LIKE', '%consultation%')->get();
    return $data;
}

场景2:基于关联模型筛选

如果确实需要通过关联模型的字段筛选Investigations数据,需先在Investigations模型中定义对应关联关系,再在whereHas中传入关联名称和闭包:

  1. 先在Investigations模型里定义关联(示例,根据实际业务调整关联类型和模型):
public function relatedModel()
{
    return $this->belongsTo(RelatedModel::class); // 替换为实际的关联逻辑
}
  1. 修改payments方法:
public function payments(){
    $data = Investigations::whereHas('relatedModel', function($query){
        $query->where('name', 'LIKE', '%consultation%');
    })->get();
    return $data;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:15:36