Laravel Eloquent получить счет - PullRequest
0 голосов
/ 04 апреля 2019

Я пытаюсь получить результат с помощью приведенного ниже запроса, но обнаружил ошибку

$getAllRequirementRecord = Requirement::with('RequirementLocation')
    ->withCount('RequirementRecruiter')
    ->withcount('Interview.candidate')
    ->get()
    ->toArray();

Вот ошибка

Вызов неопределенного метода App \ backend_model \ Requirement \ Requirement:: Interview.candidate ()

Ответы [ 2 ]

1 голос
/ 11 апреля 2019

Попробуйте этот код, он будет успешно выполнен ...

$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');
}
0 голосов
/ 04 апреля 2019

withCount ищет методы отношений в модели, для которой вы пишете запрос, Interview.candidate не похож на имя метода в классе Requirement.Если вы пытаетесь перемещаться по отношению Requirement -> Interview -> Candidate, как указано на этот ответ , вы можете определить следующие отношения на Requirement:

function interview()
{
    return $this->hasMany(Interview::class);
}

function candidate()
{
    return $this->hasManyThrough(Candidate::class, Interview::class);
}

А потом:

Tutorial::withCount(['interview', 'candidate'])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...