Mahfuz Shishir
Mahfuz Shishir

Reputation: 841

select not working in laravel eager loading after join

This code works perfectly.

InspectionClaim::with(['claimType', 'inspectionClaimReviews' => function($query){
                        $query->join('employees', 'inspection_claim_reviews.reviewer_id', '=', 'employees.id');
                        $query->join('designations', 'employees.designation_id', '=', 'designations.id');
                        //$query->select('inspection_claim_reviews.created_at', 'employees.name', 'designations.title'); // when I am adding select function it returns empty value 
                    }])
                    ->get();

But when I use select function to specify which column I need, then it returns empty.

Upvotes: 0

Views: 263

Answers (1)

kingshark
kingshark

Reputation: 315

I think the issue is not related to join.

If you use select for sub query in eager loading, you need to AT LEAST include the primary key and/or relation key (depend on the relation type)


In your case, seems like the relation between InspectionClaim and inspectionClaimReviews is one-to-many, assuming you define relation in InspectionClaim class:

public function inspectionClaimReviews()
{
    return $this->hasMany(InspectionClaimReview::class,'inspection_claim_id');
}

so when you do select, you should at least include this field inspection_claim_reviews.inspection_claim_id.

Upvotes: 1

Related Questions