Skip to content

Commit

Permalink
[5.4] add tap to query builder (#18284)
Browse files Browse the repository at this point in the history
* add tap to query builder

* formatting mistake

* formatting correction
  • Loading branch information
BrandonShar authored and taylorotwell committed Mar 9, 2017
1 parent a030889 commit a4fe12a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a4fe12a

Please sign in to comment.