Skip to content

Commit 5621260

Browse files
JosephSilbertaylorotwell
authored andcommitted
Don't require returning the query from "when" (#18422)
1 parent 89c957a commit 5621260

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/Illuminate/Database/Concerns/BuildsQueries.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,19 @@ public function first($columns = ['*'])
7474
/**
7575
* Apply the callback's query changes if the given "value" is true.
7676
*
77-
* @param bool $value
77+
* @param mixed $value
7878
* @param \Closure $callback
7979
* @param \Closure $default
8080
* @return mixed
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($this, $value) ?: $this;
8886
} elseif ($default) {
89-
$builder = $default($builder, $value);
87+
return $default($this, $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)