Skip to content

Commit 757819d

Browse files
authored
Merge pull request spatie#53 from vpratfr/find-with-uuid
Add some eloquent-ness to the trait
2 parents 0219a73 + f1a8e26 commit 757819d

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Builder.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Spatie\BinaryUuid;
4+
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
7+
class Builder extends EloquentBuilder {
8+
/**
9+
* Find a model by its primary key.
10+
*
11+
* @param mixed $id
12+
* @param array $columns
13+
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
14+
*/
15+
public function find($id, $columns = ['*'])
16+
{
17+
if (is_array($id) || $id instanceof Arrayable) {
18+
return $this->findMany($id, $columns);
19+
}
20+
21+
return $this->withUuid($id)->first($columns);
22+
}
23+
24+
/**
25+
* Find multiple models by their primary keys.
26+
*
27+
* @param \Illuminate\Contracts\Support\Arrayable|array $ids
28+
* @param array $columns
29+
* @return \Illuminate\Database\Eloquent\Collection
30+
*/
31+
public function findMany($ids, $columns = ['*'])
32+
{
33+
if (empty($ids)) {
34+
return $this->model->newCollection();
35+
}
36+
37+
return $this->withUuid($ids)->get($columns);
38+
}
39+
}

src/HasBinaryUuid.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Ramsey\Uuid\Uuid;
66
use Illuminate\Database\Eloquent\Model;
7-
use Illuminate\Database\Eloquent\Builder;
87

98
trait HasBinaryUuid
109
{
@@ -190,6 +189,11 @@ public function newQueryForRestoration($id)
190189
return $this->newQueryWithoutScopes()->whereKey(base64_decode($id));
191190
}
192191

192+
public function newEloquentBuilder($query)
193+
{
194+
return new Builder($query);
195+
}
196+
193197
public function getRouteKeyName()
194198
{
195199
$suffix = $this->getUuidSuffix();

tests/Feature/HasBinaryUuidTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,36 @@ public function it_prevents_decoding_model_key_when_it_is_not_included_in_attrib
214214

215215
$this->assertFalse(isset($array[$model->getKeyName()]));
216216
}
217+
218+
/** @test */
219+
public function it_finds_a_model_from_its_textual_uuid_too()
220+
{
221+
$model = TestModel::create();
222+
223+
$this->assertTrue($model->is(TestModel::find($model->uuid)));
224+
$this->assertTrue($model->is(TestModel::find($model->uuid_text)));
225+
}
226+
227+
/** @test */
228+
public function it_finds_many_models_from_their_textual_uuids()
229+
{
230+
$model1 = TestModel::create();
231+
$model2 = TestModel::create();
232+
$model3 = TestModel::create();
233+
234+
$this->assertCount(2, TestModel::find([$model1->uuid, $model3->uuid,]));
235+
$this->assertCount(2, TestModel::findMany([$model1->uuid, $model3->uuid,]));
236+
237+
$this->assertCount(2, TestModel::find([$model1->uuid_text, $model3->uuid_text,]));
238+
$this->assertCount(2, TestModel::findMany([$model1->uuid_text, $model3->uuid_text,]));
239+
}
240+
241+
/** @test */
242+
public function it_finds_or_fails_a_model_from_its_textual_uuid()
243+
{
244+
$model = TestModel::create();
245+
246+
$this->assertTrue($model->is(TestModel::findOrFail($model->uuid)));
247+
$this->assertTrue($model->is(TestModel::findOrFail($model->uuid_text)));
248+
}
217249
}

0 commit comments

Comments
 (0)