staudenmeir / belongs-to-through

Laravel Eloquent BelongsToThrough relationships
MIT License
1.15k stars 88 forks source link

Parent with SoftDeletes bug #31

Closed axlon closed 5 years ago

axlon commented 5 years ago

This PR fixes a bug that causes the BelongsToThrough relation to never return any results when the parent model is soft deleted.

Imagine the following scenario:

model A (uses SoftDeletes)

model B

model C

If model A has soft deletes the relation will currently generate the following query:

select `c`.*
from `c`
left join `b` on `c`.`id` = `b`.`c_id`
left join `a` on `b`.`id` = `a`.`b_id`
where `a`.`id` = ? and `a`.`id` is not null and `a`.`deleted_at` is null

After this PR the relation will generate the following query:

select `c`.*
from `c`
left join `b` on `c`.`id` = `b`.`c_id`
left join `a` on `b`.`id` = `a`.`b_id`
where `a`.`id` = ? and `a`.`id` is not null

Note that any models that are not the parent will still have soft delete checks added to the query if they use the SoftDeletes trait.

znck commented 5 years ago

Looking for maintainers. #32

staudenmeir commented 5 years ago

Thanks, I'll take a look at it in the next days.

axlon commented 5 years ago

@staudenmeir any update?

staudenmeir commented 5 years ago

Sorry, still working on the next version. I'll release it this week.

staudenmeir commented 5 years ago

Thanks! Check out v2.4.