staudenmeir / eloquent-has-many-deep

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

Use different id's for `laravel_through_key` and `where` clause? #144

Closed tonyv69 closed 2 years ago

tonyv69 commented 3 years ago

Hi, is it possible to use different id's for laravel_through_key and where clause?

example below (sql generated from hasManyDeep):

select `asl_purchase_order_options`.*, `customer_purchase_order_options`.`id1` as `laravel_through_key`
from `asl_purchase_order_options`
         inner join `customer_account_purchase_orders`
                    on `customer_account_purchase_orders`.`id6` = `asl_purchase_order_options`.`id3`
         inner join `customer_purchase_order_options`
                    on `customer_purchase_order_options`.`id5` = `customer_account_purchase_orders`.`id2`
where `customer_purchase_order_options`.`id1` in (?)

as you can see the above id1 (first id in foreignKeys array in hasManyDeep()) is used twice for laravel_through_key and where clause.

is it possible to use different id's?

Hope that make sense.

Thanks Tony

staudenmeir commented 3 years ago

Why would look like to change that? Do you want to select columns from an intermediate/pivot table?

The laravel_through_key column is required for eager loading to work.

tonyv69 commented 3 years ago

Thanks for the link and quick reply. I'll see if I can use that method to achieve what I want.

I want to use the id of the pivot table as the through key, and the id in the where clause as the parent model id the one that's normally used.

I'm trying to pass through two pivot tables to get to the data I need.

staudenmeir commented 2 years ago

Did it work for you?