Skip to content

Commit a4fe12a

Browse files
BrandonShartaylorotwell
authored andcommitted
[5.4] add tap to query builder (#18284)
* add tap to query builder * formatting mistake * formatting correction
1 parent a030889 commit a4fe12a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Illuminate/Database/Query/Builder.php

+11
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,17 @@ public function when($value, $callback, $default = null)
478478
return $builder;
479479
}
480480

481+
/**
482+
* Pass the query to a given callback.
483+
*
484+
* @param \Closure $callback
485+
* @return \Illuminate\Database\Query\Builder
486+
*/
487+
public function tap($callback)
488+
{
489+
return $this->when(true, $callback);
490+
}
491+
481492
/**
482493
* Merge an array of where clauses and bindings.
483494
*

tests/Database/DatabaseQueryBuilderTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ public function testWhenCallbackWithDefault()
166166
$this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings());
167167
}
168168

169+
public function testTapCallback()
170+
{
171+
$callback = function ($query) {
172+
return $query->where('id', '=', 1);
173+
};
174+
175+
$builder = $this->getBuilder();
176+
$builder->select('*')->from('users')->tap($callback)->where('email', 'foo');
177+
$this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql());
178+
}
179+
169180
public function testBasicWheres()
170181
{
171182
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)