Nikunj K.
Nikunj K.

Reputation: 9199

Laravel Eloquent get count

I am trying to get result with the below query but I found error

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

Here is Error

Call to undefined method App\backend_model\Requirement\Requirement::Interview.candidate()

Upvotes: 1

Views: 227

Answers (2)

Harshil Vasani
Harshil Vasani

Reputation: 28

Try this code it will be execute successfully...

$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');
}

Upvotes: 1

namelivia
namelivia

Reputation: 2745

withCount looks for relationship methods on the model you are writing the query for, Interview.candidate doesn't look like a method name on the Requirement class. If you are trying to navigate through the relationship Requirement -> Interview -> Candidate as pointed on this answer, you can define the following relationships on Requirement:

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

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

And then:

Tutorial::withCount(['interview', 'candidate'])

Upvotes: 0

Related Questions