Skip to content

Commit

Permalink
Add whereKeyNot to Eloquent Builder (#20817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Komarev authored and taylorotwell committed Aug 29, 2017
1 parent 418d5f6 commit 21f4d19
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,23 @@ public function whereKey($id)
return $this->where($this->model->getQualifiedKeyName(), '=', $id);
}

/**
* Add a where clause on the primary key to the query.
*
* @param mixed $id
* @return $this
*/
public function whereKeyNot($id)
{
if (is_array($id) || $id instanceof Arrayable) {
$this->query->whereNotIn($this->model->getQualifiedKeyName(), $id);

return $this;
}

return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
}

/**
* Add a basic where clause to the query.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,45 @@ public function testWhereKeyMethodWithCollection()
$builder->whereKey($collection);
}

public function testWhereKeyNotMethodWithInt()
{
$model = $this->getMockModel();
$builder = $this->getBuilder()->setModel($model);
$keyName = $model->getQualifiedKeyName();

$int = 1;

$builder->getQuery()->shouldReceive('where')->once()->with($keyName, '!=', $int);

$builder->whereKeyNot($int);
}

public function testWhereKeyNotMethodWithArray()
{
$model = $this->getMockModel();
$builder = $this->getBuilder()->setModel($model);
$keyName = $model->getQualifiedKeyName();

$array = [1, 2, 3];

$builder->getQuery()->shouldReceive('whereNotIn')->once()->with($keyName, $array);

$builder->whereKeyNot($array);
}

public function testWhereKeyNotMethodWithCollection()
{
$model = $this->getMockModel();
$builder = $this->getBuilder()->setModel($model);
$keyName = $model->getQualifiedKeyName();

$collection = new Collection([1, 2, 3]);

$builder->getQuery()->shouldReceive('whereNotIn')->once()->with($keyName, $collection);

$builder->whereKeyNot($collection);
}

protected function mockConnectionForModel($model, $database)
{
$grammarClass = 'Illuminate\Database\Query\Grammars\\'.$database.'Grammar';
Expand Down

0 comments on commit 21f4d19

Please sign in to comment.