Skip to content

Commit 873dd23

Browse files
committed
Don't require returning the query from "when"
1 parent 36b2550 commit 873dd23

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,12 @@ public function first($columns = ['*'])
8181
*/
8282
public function when($value, $callback, $default = null)
8383
{
84-
$builder = $this;
85-
8684
if ($value) {
87-
$builder = $callback($builder, $value);
85+
return $callback($builder, $value) ?: $this;
8886
} elseif ($default) {
89-
$builder = $default($builder, $value);
87+
return $default($builder, $value) ?: $this;
9088
}
9189

92-
return $builder;
90+
return $this;
9391
}
9492
}

tests/Database/DatabaseQueryBuilderTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testWhenCallback()
135135
$callback = function ($query, $condition) {
136136
$this->assertTrue($condition);
137137

138-
return $query->where('id', '=', 1);
138+
$query->where('id', '=', 1);
139139
};
140140

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

155-
return $query->where('id', '=', 1);
155+
$query->where('id', '=', 1);
156156
};
157157

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

161-
return $query->where('id', '=', 2);
161+
$query->where('id', '=', 2);
162162
};
163163

164164
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)