Laravel 拉雷维尔口若悬河,数一数

Laravel 拉雷维尔口若悬河,数一数,laravel,laravel-5,eloquent,model,Laravel,Laravel 5,Eloquent,Model,我试图用下面的查询得到结果,但我发现了错误 $getAllRequirementRecord=Requirement::with('RequirementLocation')) ->withCount('RequirementRecruiter') ->withcount('面试.候选人') ->得到() ->toArray(); 这是一个错误 调用未定义的方法 App\backend\u model\Requirement\Requirement::Interview.candidate()

我试图用下面的查询得到结果,但我发现了错误

$getAllRequirementRecord=Requirement::with('RequirementLocation'))
->withCount('RequirementRecruiter')
->withcount('面试.候选人')
->得到()
->toArray();
这是一个错误

调用未定义的方法 App\backend\u model\Requirement\Requirement::Interview.candidate()


withCount
在您为之编写查询的模型上查找关系方法,
面试。候选人
看起来不像
需求
类上的方法名称。 如果您试图浏览关系
需求
->
面试
->
候选人
as,您可以在
需求
上定义以下关系:

功能面试()
{
返回$this->hasMany(访谈::类);
}
函数候选者()
{
返回$this->hasManyThrough(候选人::类,面试::类);
}
然后:

Tutorial::withCount([“面试”、“应聘者])

尝试此代码,它将成功执行

$getAllRequirementRecord = Requirement::with('RequirementLocation')->get();
foreach($getAllRequirementRecord as $a){
    $recruiter_count = RequirementRecruiter::select('agency_id','requirement_id')    
        ->where('requirement_id',$a->id)->distinct('agency_id')->count('agency_id');

    $candidate_count = CandidateReferance::select('candidate_id','requirement_id')
        ->where('requirement_id',$id)->distinct('candidate_id')enter code here
        ->count('candidate_id');
}

你的
面试
类是否有一个候选函数?你的代码中有一个类型,
withcount
应该是
withcount
@LucasPiazzi PHP对函数名不区分大小写。此外,如果是,错误将是undefined method withcount。我没有说这是导致问题的原因,只是指出他以不同的风格编写了相同的方法。但是谢谢你提供的信息。
withCount()
不支持嵌套关系。非常感谢你指出了这一点,我想是的。我将编辑我的答案。