Skip to content

Commit

Permalink
Don't require returning the query from "when"
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Mar 19, 2017
1 parent 36b2550 commit 2e3f9ab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,12 @@ public function first($columns = ['*'])
*/
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 2e3f9ab

Please sign in to comment.