Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellard committed Oct 24, 2024
1 parent 55761b0 commit c93cde0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
6 changes: 5 additions & 1 deletion tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c93cde0

Please sign in to comment.