Skip to content

Commit

Permalink
styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Cellard committed Oct 24, 2024
1 parent 01aaed5 commit 078b48f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public function whereHas($relation, ?Closure $callback = null, $operator = '>=',
* @param \Closure|null $callback
* @param string $operator
* @param int $count
*
* @return $this
*/
public function whereHasPivot($relation, Closure $callback, $operator = '>=', $count = 1)
Expand All @@ -172,10 +171,11 @@ function (Builder $builder) use ($callback, $relation) {

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

return $builder;
}

throw new InvalidArgumentException("Only BelongsToMany relations are applicable.");
throw new InvalidArgumentException('Only BelongsToMany relations are applicable.');
}, $operator, $count
);
}
Expand Down Expand Up @@ -228,10 +228,11 @@ function (Builder $builder) use ($callback, $relation) {

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

return $builder;
}

throw new InvalidArgumentException("Only BelongsToMany relations are applicable.");
throw new InvalidArgumentException('Only BelongsToMany relations are applicable.');
}, $operator, $count
);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1326,41 +1326,41 @@ public function testWhereHasPivot()
]);

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

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

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

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

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

$this->assertNull($relationTag);
}

public function testWhereHasPivotForwardCall()
{
$tag = Tag::create(['id' => 1, 'name' => Str::random()])->fresh();
$post = Post::create(['id' => 2, 'title' => Str::random()]);
$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'],
]);

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->whereKey(2)
fn (BelongsToMany $builder) => $builder->whereKey($post->getKey())
)->first();

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

$relationTag = $post->tags()->whereHasPivot('posts',
fn(BelongsToMany $builder) => $builder->whereKey(1)
fn (BelongsToMany $builder) => $builder->whereKey($post->getKey() + 1)
)->first();

$this->assertNull($relationTag);
Expand Down
5 changes: 1 addition & 4 deletions tests/Integration/Database/EloquentBelongsToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
use Illuminate\Tests\Integration\Database\EloquentBelongsToManyTest\Post;
use Illuminate\Tests\Integration\Database\EloquentBelongsToManyTest\Tag;

class EloquentBelongsToTest extends DatabaseTestCase
{
Expand Down Expand Up @@ -138,7 +135,7 @@ public function testWhereHasPivotWrongType()

$this->expectException(\InvalidArgumentException::class);

$user->whereHasPivot('parent', fn(BelongsToMany $pivot) => $pivot)->first();
$user->whereHasPivot('parent', fn (BelongsToMany $pivot) => $pivot)->first();
}
}

Expand Down

0 comments on commit 078b48f

Please sign in to comment.