From 0c32d08d78622b3f6e365c23d9351acfafcc9071 Mon Sep 17 00:00:00 2001 From: Brandon Shar Date: Thu, 9 Mar 2017 11:04:35 -0500 Subject: [PATCH 1/3] add tap to query builder --- src/Illuminate/Database/Query/Builder.php | 10 ++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 007c72f4e1e0..417280aaa626 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -478,6 +478,16 @@ 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(); From 66062ace2d652c981520a1d2ea82aebd9d61894f Mon Sep 17 00:00:00 2001 From: Brandon Shar Date: Thu, 9 Mar 2017 11:12:54 -0500 Subject: [PATCH 2/3] formatting mistake --- src/Illuminate/Database/Query/Builder.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 417280aaa626..4251a08660aa 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -479,12 +479,13 @@ public function when($value, $callback, $default = null) } /** - * Pass the query to a given callback + * Pass the query to a given callback. * * @param \Closure $callback * @return \Illuminate\Database\Query\Builder */ - public function tap($callback) { + public function tap($callback) + { return $this->when(true, $callback); } From 9d5a1086862307f32c427157580cb3ea41a1fe5c Mon Sep 17 00:00:00 2001 From: Brandon Shar Date: Thu, 9 Mar 2017 11:13:55 -0500 Subject: [PATCH 3/3] formatting correction --- src/Illuminate/Database/Query/Builder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 4251a08660aa..2f5d0db7ee4e 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -480,7 +480,7 @@ public function when($value, $callback, $default = null) /** * Pass the query to a given callback. - * + * * @param \Closure $callback * @return \Illuminate\Database\Query\Builder */