Skip to content

Commit

Permalink
Implementing having shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
dbpolito committed Jan 5, 2017
1 parent f30c564 commit 042ff3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,20 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
{
$type = 'Basic';

// Here we will make some assumptions about the operator. If only 2 values are
// passed to the method, we will assume that the operator is an equals sign
// and keep going. Otherwise, we'll require the operator to be passed in.
list($value, $operator) = $this->prepareValueAndOperator(
$value, $operator, func_num_args() == 2
);

// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately.
if ($this->invalidOperator($operator)) {
list($value, $operator) = [$operator, '='];
}

$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ public function testHavings()
$this->assertEquals('select "category", count(*) as "total" from "item" where "department" = ? group by "category" having "total" > ?', $builder->toSql());
}

public function testHavingShortcut()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->having('email', 1)->orHaving('email', 2);
$this->assertEquals('select * from "users" having "email" = ? or "email" = ?', $builder->toSql());
}

public function testHavingFollowedBySelectGet()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 042ff3c

Please sign in to comment.