Open Sajid-al-islam opened 4 days ago
Marking as draft since tests are failing.
@Sajid-al-islam I was looking at your PR, the test fails because the withTrashed function in MorphTo thinks a query-builder for the model is passed, but in reality, the relation is passed. The argument in the callback ($query)
(Eloquent/Relations/MorphTo.php:349) changes from "Illuminate\Database\Eloquent\Builder" to "Illuminate\Database\Eloquent\Relations\MorphTo", where the 'withTrashed' macro never exists.
I think this might be a way to proceed, but I think the macro-check needs to be done differently. The 'withoutTrashed' and 'onlyTrashed' in the same class use the same logic. Because this changes the real type of the argument in the callback from Eloquent\Builder
to Eloquent\Relations\..
I think this must be treated with care and be seen as a breaking change.
@rikvdh thanks, I will look into it
Fixes #53292
Description
When using the
when()
method on aBelongsToMany
relationship, the query builder instance was being returned instead of the relationship instance, preventing the use of relationship-specific methods likewherePivot
.This PR fixes the issue by maintaining the relationship instance within the
when()
method specifically forBelongsToMany
relationships while preserving existing behavior for other relationship types.Example Usage