Skip to content

Commit 305c490

Browse files
committed
Update find* methods to work with textual versions of the UUIDs
1 parent 0396908 commit 305c490

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/HasBinaryUuid.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ protected static function bootHasBinaryUuid()
1919
});
2020
}
2121

22+
public static function find($id, $columns = ['*'])
23+
{
24+
if (\is_array($id) || $id instanceof Arrayable) {
25+
return static::findMany($id, $columns);
26+
}
27+
28+
return static::withUuid($id)->first($columns);
29+
}
30+
31+
public static function findMany($ids, $columns = ['*'])
32+
{
33+
if (empty($ids)) {
34+
return static::newModelInstance()->newCollection();
35+
}
36+
37+
return static::withUuid($ids)->get($columns);
38+
}
39+
2240
public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder
2341
{
2442
if ($field) {

tests/Feature/HasBinaryUuidTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,27 @@ public function it_prevents_decoding_model_key_when_it_is_not_included_in_attrib
206206

207207
$this->assertFalse(isset($array[$model->getKeyName()]));
208208
}
209+
210+
/** @test */
211+
public function it_finds_a_model_from_its_textual_uuid_too()
212+
{
213+
$model = TestModel::create();
214+
215+
$this->assertTrue($model->is(TestModel::find($model->uuid)));
216+
$this->assertTrue($model->is(TestModel::find($model->uuid_text)));
217+
}
218+
219+
/** @test */
220+
public function it_finds_many_models_from_their_textual_uuids()
221+
{
222+
$model1 = TestModel::create();
223+
$model2 = TestModel::create();
224+
$model3 = TestModel::create();
225+
226+
$this->assertCount(2, TestModel::find([$model1->uuid, $model3->uuid,] ));
227+
$this->assertCount(2, TestModel::findMany([$model1->uuid, $model3->uuid,] ));
228+
229+
$this->assertCount(2, TestModel::find([$model1->uuid_text, $model3->uuid_text,] ));
230+
$this->assertCount(2, TestModel::findMany([$model1->uuid_text, $model3->uuid_text,] ));
231+
}
209232
}

0 commit comments

Comments
 (0)