[12.x] Add flag to disable where clauses for withAttributes
method on Eloquent Builder
#55199
+722
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
The
withAttributes
method (documented here) is very helpful for adding attributes to both thewhere
clauses as well as the attributes when a model is made from the relationship.However, I ran into a situation where I needed to set an attribute when the relationship is created, but not constrain the relationship to only rows where the attribute is set to that value.
The
$pendingAttributes
property already exists on the Eloquent builder and all the logic to add the pending attributes to the new models also exist.What
A new argument (
$asConditions
, defaults totrue
) was addedwithAttributes
method on the Eloquent builder to determine whether the attributes passed into the method will also create where clauses.Implementation:
Usage:
See previous PR content
Why
Another attempt at #55178. Instead of adding the
pendingAttributes
method, Taylor said he would rather have this passed as a named argument towithAttributes
(see comment).The
withAttributes
method (documented here) is very helpful for adding attributes to both thewhere
clauses as well as the attributes when a model is made from the relationship.However, I ran into a situation where I needed to set an attribute when the relationship is created, but not constrain the relationship to only rows where the attribute is set to that value.
The
$pendingAttributes
property already exists on the Eloquent builder and all the logic to add the pending attributes to the new models also exist.What
This led to me creating thependingAttributes
method to do the same thing aswithAttributes
but not touch thewhere
clauses.This led to me adding thepending
named argument to thewithAttributes
method on the Eloquent builder.I added an
$addWheres
flag (probably should be renamed) to thewithAttributes
method on the Eloquent builder to determine whether the attributes passed into the method will also create where clauses. The default value istrue
.Tests:
tests/Database/DatabaseEloquentBelongsToManyWithAttributesPendingTest.php
Copied from
DatabaseEloquentBelongsToManyWithAttributesTest.php
and made sure only the relationalwhere
clauses existtests/Database/DatabaseEloquentHasOneOrManyWithAttributesPendingTest.php
Copied from
DatabaseEloquentHasOneOrManyWithAttributesTest.php
and made sure only the relationalwhere
clauses existtests/Database/DatabaseEloquentWithAttributesPendingTest.php
Copied from
DatabaseEloquentWithAttributesTest.php
and made sure nowhere
clauses existAlternative
Edit: Implemented the alternative! Current interface is
withAttributes(Expression|array|string $attributes, $value = null, $addWheres = true)
One downside to using a named parameter to pass the
pending
attributes, is that the$attributes
argument is still required, meaning we would need to do->withAttributes([], pending: ['key' => 'value'])
to use it. After creating this commit, I had the idea that we could simply add a$applyWhere
or$addWhere
towithAttributes
to dictate whether the where clauses are added (true
by default)Taylor, if you like the idea of changing
withAttributes
towithAttributes(Expression|array|string $attributes, $value = null, $addWheres = true)
overwithAttributes(Expression|array|string $attributes, $value = null, array $pending = [])
, please let me know before closing the PR and I will update it.