Skip to content

[12.x] Lazily evaluate value for constraints in HasOneOrManyThrough#59231

Merged
taylorotwell merged 2 commits intolaravel:12.xfrom
Jacobs63:lazy-local-value
Mar 16, 2026
Merged

[12.x] Lazily evaluate value for constraints in HasOneOrManyThrough#59231
taylorotwell merged 2 commits intolaravel:12.xfrom
Jacobs63:lazy-local-value

Conversation

@Jacobs63
Copy link
Contributor

@Jacobs63 Jacobs63 commented Mar 16, 2026

In a scenario, where:

  1. Primary keys are casted, in our case we're using value objects representing an integer with a value >= 1 called PositiveInteger
  2. Using a relation that extends HasOneOrManyThrough results in an exception

The reason is, that at the moment of calling addConstraints in the context of constructor of either HasOneThrough or HasManyThrough, the $farParent is empty.
This means our cast, which is highly strict, results in an exception.

When debugging I've noticed that, unlike any other relation's addContraints method, this is for some (unknown) reason evaluated even if the static::$constraints is set to false which, coincidentally, breaks our application.

This PR changes the behaviour in \Illuminate\Database\Eloquent\Relations\HasOneOrManyThrough::addConstraints to resolve the value for constraint in the if statement, therefore fixing this inconsistency (and our bug).

@Jacobs63
Copy link
Contributor Author

The failing tests seem to be unrelated to this PR, as they're also failing in the 12.x branch.

@taylorotwell taylorotwell merged commit 4fa9548 into laravel:12.x Mar 16, 2026
22 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants