Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add existsOr and doesntExistOr to the querybuilder #30495

Merged
merged 3 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add existsOr and doesntExistOr to the querybuilder
  • Loading branch information
SjorsO committed Nov 1, 2019
commit 901f39f80cb936164a79f35185f9eff0b1c121ea
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,28 @@ public function doesntExist()
return ! $this->exists();
}

/**
* Execute the given callback if no rows exist for the current query.
*
* @param \Closure $callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to typehint Closure|bool

* @return mixed
*/
public function existsOr(Closure $callback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow all types here (should only be closure and bool, but there is some time till union types will be available).

{
return $this->exists() ? true : $callback();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And return $this->exists() ?: value($callback)

I prefer ?: over ? true : but maybe that's just me.

}

/**
* Execute the given callback if rows exist for the current query.
*
* @param \Closure $callback
* @return mixed
*/
public function doesntExistOr(Closure $callback)
{
return $this->doesntExist() ? true : $callback();
}

/**
* Retrieve the "count" result of the query.
*
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,38 @@ public function testSqlServerExists()
$this->assertTrue($results);
}

public function testExistsOr()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 1]]);
$results = $builder->from('users')->doesntExistOr(function () {
return 123;
});
$this->assertSame(123, $results);
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 0]]);
$results = $builder->from('users')->doesntExistOr(function () {
throw new RuntimeException();
});
$this->assertTrue($results);
}

public function testDoesntExistsOr()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 0]]);
$results = $builder->from('users')->existsOr(function () {
return 123;
});
$this->assertSame(123, $results);
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->andReturn([['exists' => 1]]);
$results = $builder->from('users')->existsOr(function () {
throw new RuntimeException();
});
$this->assertTrue($results);
}

public function testAggregateResetFollowedByGet()
{
$builder = $this->getBuilder();
Expand Down