From c93cde0c8d7d6e03da93350fc271f57f2d3fc82e Mon Sep 17 00:00:00 2001 From: pm Date: Thu, 24 Oct 2024 14:16:12 +0300 Subject: [PATCH] improvements --- .../Database/Eloquent/Concerns/QueriesRelationships.php | 3 ++- tests/Integration/Database/EloquentBelongsToManyTest.php | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index 31ac86ec3da..b780cf55a68 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -168,7 +168,8 @@ public function whereHasPivot($relation, Closure $callback, $operator = '>=', $c { return $this->has($relation, $operator, $count, $boolean, function (Builder $builder) use ($callback, $relation) { - $relation = is_string($relation) ? $this->getRelation($relation) : $relation; + // As we modify given relation, will use its clone + $relation = is_string($relation) ? $this->getRelationWithoutConstraints($relation) : clone $relation; if ($relation instanceof BelongsToMany) { call_user_func($callback, $relation->setQuery($builder->getQuery())); diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index 2504adda0da..e2769d08b4a 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -1359,11 +1359,15 @@ public function testWhereHasPivotAsObject() ['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'rab'], ]); - $relationTag = $post->tags()->whereHasPivot($tag->posts(), + $relation = $tag->posts(); + $originalQuery = $relation->getQuery(); + + $relationTag = $post->tags()->whereHasPivot($relation, fn (BelongsToMany $builder) => $builder->wherePivot('flag', 'foo') )->first(); $this->assertEquals($relationTag->getAttributes(), $tag->getAttributes()); + $this->assertEquals($originalQuery, $relation->getQuery()); } public function testWhereHasPivotForwardCall()