Skip to content

Commit

Permalink
group code
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellard committed Oct 24, 2024
1 parent 078b48f commit c4ea372
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
39 changes: 14 additions & 25 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,28 @@ public function whereHas($relation, ?Closure $callback = null, $operator = '>=',
/**
* Add a relationship count / exists condition to the pivot relation with where clauses.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\BelongsToMany<*, *, *>|string $relation
* @param \Closure|null $callback
* @param string $operator
* @param int $count
* @param string $boolean
* @return $this
*/
public function whereHasPivot($relation, Closure $callback, $operator = '>=', $count = 1)
public function whereHasPivot($relation, Closure $callback, $operator = '>=', $count = 1, $boolean = 'and')
{
return $this->whereHas($relation,
return $this->has($relation, $operator, $count, $boolean,
function (Builder $builder) use ($callback, $relation) {
$relation = $this->getRelation($relation);

if ($relation instanceof BelongsToMany) {
call_user_func($callback, $relation->setQuery($builder->getQuery()));
$relation = is_string($relation) ? $this->getRelation($relation) : $relation;

return $builder;
}
if ($relation instanceof BelongsToMany) {
call_user_func($callback, $relation->setQuery($builder->getQuery()));

throw new InvalidArgumentException('Only BelongsToMany relations are applicable.');
}, $operator, $count
);
return $builder;
}

throw new InvalidArgumentException('Only BelongsToMany relations are applicable.');
});
}

/**
Expand Down Expand Up @@ -214,27 +215,15 @@ public function orWhereHas($relation, ?Closure $callback = null, $operator = '>=
/**
* Add a relationship count / exists condition to the pivot relation with where clauses and an "or".
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\BelongsToMany<*, *, *>|string $relation
* @param \Closure|null $callback
* @param string $operator
* @param int $count
* @return $this
*/
public function orWhereHasPivot($relation, Closure $callback, $operator = '>=', $count = 1): static
{
return $this->orWhereHas($relation,
function (Builder $builder) use ($callback, $relation) {
$relation = $this->getRelation($relation);

if ($relation instanceof BelongsToMany) {
call_user_func($callback, $relation->setQuery($builder->getQuery()));

return $builder;
}

throw new InvalidArgumentException('Only BelongsToMany relations are applicable.');
}, $operator, $count
);
return $this->whereHasPivot($relation, $callback, $operator, $count, 'or');
}

/**
Expand Down
28 changes: 25 additions & 3 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1331,9 +1331,14 @@ public function testWhereHasPivot()

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());

$relationTag = $post->tags()->whereHasPivot('posts',
fn (BelongsToMany $builder) => $builder->wherePivotIn('flag', ['bar', 'rab'])
)->first();
$relationTag = $post->tags()
->whereHasPivot('posts',
fn (BelongsToMany $builder) => $builder->wherePivot('flag', 'bar')
)
->orWhereHasPivot('posts',
fn (BelongsToMany $builder) => $builder->wherePivot('flag', 'rab')
)
->first();

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());

Expand All @@ -1344,6 +1349,23 @@ public function testWhereHasPivot()
$this->assertNull($relationTag);
}

public function testWhereHasPivotAsObject()
{
$tag = Tag::create(['name' => Str::random()])->fresh();
$post = Post::create(['title' => Str::random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'foo'],
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'rab'],
]);

$relationTag = $post->tags()->whereHasPivot($tag->posts(),
fn (BelongsToMany $builder) => $builder->wherePivot('flag', 'foo')
)->first();

$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());
}

public function testWhereHasPivotForwardCall()
{
$tag = Tag::create(['name' => Str::random()])->fresh();
Expand Down

0 comments on commit c4ea372

Please sign in to comment.