diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 007c72f4e1e0..2f5d0db7ee4e 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -478,6 +478,17 @@ public function when($value, $callback, $default = null) return $builder; } + /** + * Pass the query to a given callback. + * + * @param \Closure $callback + * @return \Illuminate\Database\Query\Builder + */ + public function tap($callback) + { + return $this->when(true, $callback); + } + /** * Merge an array of where clauses and bindings. * diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 94662dcee54b..eb0fdc1f18b7 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -166,6 +166,17 @@ public function testWhenCallbackWithDefault() $this->assertEquals([0 => 2, 1 => 'foo'], $builder->getBindings()); } + public function testTapCallback() + { + $callback = function ($query) { + return $query->where('id', '=', 1); + }; + + $builder = $this->getBuilder(); + $builder->select('*')->from('users')->tap($callback)->where('email', 'foo'); + $this->assertEquals('select * from "users" where "id" = ? and "email" = ?', $builder->toSql()); + } + public function testBasicWheres() { $builder = $this->getBuilder();