Skip to content

Commit c64718f

Browse files
authored
Merge pull request #17 from BirdDGP/where-in-fix
Add whereKey to extend on Eloquent Builder
2 parents 52fe0b7 + 856dffe commit c64718f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Database/EloquentBuilder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
6060
return $this;
6161
}
6262

63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function whereKey($id)
67+
{
68+
if (is_array($id) || $id instanceof Arrayable) {
69+
$this->whereIn($this->model->getQualifiedKeyName(), $id);
70+
71+
return $this;
72+
}
73+
74+
return parent::whereKey($id);
75+
}
76+
6377
/**
6478
* {@inheritdoc}
6579
*/

tests/Integration/MutatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ public function test_find()
8888
$this->assertEquals($id, $p->id);
8989
}
9090

91+
public function test_array_of_find()
92+
{
93+
$id = Uuid::uuid1()->toString();
94+
$model = (new TestModel())->create(['id' => $id, 'name' => 'Name A']);
95+
$id2 = Uuid::uuid1()->toString();
96+
$model = (new TestModel())->create(['id' => $id2, 'name' => 'Name B']);
97+
$p = $model->find([$id, $id2]);
98+
$this->assertEquals(2, $p->count());
99+
$this->assertEquals($id, $p[0]->id);
100+
$this->assertEquals($id2, $p[1]->id);
101+
}
102+
91103
public function test_non_mutated_columns()
92104
{
93105
$id = Uuid::uuid1()->toString();
@@ -111,6 +123,18 @@ public function test_where_in()
111123
$this->assertEquals(2, $p->count());
112124
}
113125

126+
public function test_where_key()
127+
{
128+
$id = Uuid::uuid1()->toString();
129+
$id2 = Uuid::uuid1()->toString();
130+
$model = (new TestModel())->create(['id' => $id, 'name' => 'A chair']);
131+
$model2 = (new TestModel())->create(['id' => $id2, 'name' => 'A table']);
132+
$p = $model->whereKey([$id, $id2])->get();
133+
$this->assertEquals(2, $p->count());
134+
$this->assertEquals($id, $p[0]->id);
135+
$this->assertEquals($id2, $p[1]->id);
136+
}
137+
114138
public function test_update()
115139
{
116140
$id = Uuid::uuid1()->toString();

0 commit comments

Comments
 (0)