staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

How to get actual model Id? #90

Closed byrondcortez closed 4 years ago

byrondcortez commented 4 years ago

I have this hasManyDeep relationship:

return $this->hasManyDeep(
            '\App\ApprovalItemEmployee',
            ['\App\ApprovalRequest', '\App\ApprovalLevel', '\App\ApprovalItem'],
            [
                'id',
                'approval_request_id',
                'approval_level_id',
                'approval_item_id',
            ],
            [
                'approval_request_id',
                'id',
                'id',
                'id',
            ]
        );

when I include this relationship with (::with), I get the id of \App\ApprovalRequest. Is there a way for me to get \App\ApprovalItemEmployee's id instead?

staudenmeir commented 4 years ago

How are you using the relationship?

What SQL does the query execute when you log it with \DB::enableQueryLog(); [...] dump(\DB::getQueryLog()); or a tool like debugbar?

byrondcortez commented 4 years ago

Hi, I got it. I updated the query to alias the id of the target child:

$query = \App\LoanRequest::with([
            'approvers' => function($q) {
                $q->select('approval_item_employee.id as approval_item_employee_id', 'can_approve', 'approver_id', 'remarks', 'attachments', 'approval_item_employee.updated_at');
            }, 
        ])

Thank you for this library!