Skip to content

Commit

Permalink
Don't require returning the query from "when" (#18422)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber authored and taylorotwell committed Mar 20, 2017
1 parent 89c957a commit 5621260
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,19 @@ public function first($columns = ['*'])
/**
* Apply the callback's query changes if the given "value" is true.
*
* @param bool $value
* @param mixed $value
* @param \Closure $callback
* @param \Closure $default
* @return mixed
*/
public function when($value, $callback, $default = null)
{
$builder = $this;

if ($value) {
$builder = $callback($builder, $value);
return $callback($this, $value) ?: $this;
} elseif ($default) {
$builder = $default($builder, $value);
return $default($this, $value) ?: $this;
}

return $builder;
return $this;
}
}
6 changes: 3 additions & 3 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testWhenCallback()
$callback = function ($query, $condition) {
$this->assertTrue($condition);

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

$builder = $this->getBuilder();
Expand All @@ -152,13 +152,13 @@ public function testWhenCallbackWithDefault()
$callback = function ($query, $condition) {
$this->assertEquals($condition, 'truthy');

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

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

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

$builder = $this->getBuilder();
Expand Down

0 comments on commit 5621260

Please sign in to comment.