From 042ff3c396e000ec456f7757e9e595546b1e9022 Mon Sep 17 00:00:00 2001 From: Daniel Polito Date: Thu, 5 Jan 2017 11:56:43 -0200 Subject: [PATCH] Implementing having shortcut --- src/Illuminate/Database/Query/Builder.php | 14 ++++++++++++++ tests/Database/DatabaseQueryBuilderTest.php | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index c69325201c01..9cd683d76506 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -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) { diff --git a/tests/Database/DatabaseQueryBuilderTest.php b/tests/Database/DatabaseQueryBuilderTest.php index 542e68497df1..100746474784 100755 --- a/tests/Database/DatabaseQueryBuilderTest.php +++ b/tests/Database/DatabaseQueryBuilderTest.php @@ -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();