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

Add query buider 'unless' method (inverse of 'when') #19738

Merged
merged 1 commit into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public function when($value, $callback, $default = null)
return $this;
}

/**
* Apply the callback's query changes if the given "value" is false.
*
* @param mixed $value
* @param callable $callback
* @param callable $default
* @return mixed
*/
public function unless($value, $callback, $default = null)
{
if (! $value) {
return $callback($this, $value) ?: $this;
} elseif ($default) {
return $default($this, $value) ?: $this;
}

return $this;
}

/**
* Create a new length-aware paginator instance.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,65 @@ public function testWhenCallbackWithDefault()
$this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
}

public function testUnlessCallback()
{
$callback = function ($query, $condition) {
$this->assertFalse($condition);

$query->where('id', '=', 1);
};

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(false, $callback)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(true, $callback)->where('email', 'foo');
$this->assertEquals('select * from "users" where "email" = ?', $builder->toSql());
}

public function testUnlessCallbackWithReturn()
{
$callback = function ($query, $condition) {
$this->assertFalse($condition);

return $query->where('id', '=', 1);
};

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(false, $callback)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(true, $callback)->where('email', 'foo');
$this->assertEquals('select * from "users" where "email" = ?', $builder->toSql());
}

public function testUnlessCallbackWithDefault()
{
$callback = function ($query, $condition) {
$this->assertEquals($condition, 0);

$query->where('id', '=', 1);
};

$default = function ($query, $condition) {
$this->assertEquals($condition, 'truthy');

$query->where('id', '=', 2);
};

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless(0, $callback, $default)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
$this->assertEquals([0 => 1, 1 => 'foo'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->unless('truthy', $callback, $default)->where('email', 'foo');
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
$this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
}

public function testTapCallback()
{
$callback = function ($query) {
Expand Down