Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 70a8d93

Browse files
authored
Merge pull request #36 from slashequip/master
Allow for Route Model Binding
2 parents 6f759e3 + 258b7c9 commit 70a8d93

File tree

5 files changed

+82
-21
lines changed

5 files changed

+82
-21
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ class TestModel extends Model
5050
}
5151
```
5252

53-
If don't like the primary key named `uuid` you can leave off the `HasUuidPrimaryKey` trait and manually specify `$primaryKey`. Don't forget set `$incrementing` to false.
53+
If don't like the primary key named `uuid` you can overwrite the `getKeyName` method to manually specify the primary key name.
5454

5555
```php
5656
use Illuminate\Database\Eloquent\Model;
5757
use Spatie\BinaryUuid\HasBinaryUuid;
5858

5959
class TestModel extends Model
6060
{
61-
use HasBinaryUuid;
61+
use HasBinaryUuid,
62+
HasUuidPrimaryKey;
6263

63-
public $incrementing = false;
64-
65-
public $primaryKey = 'uuid';
64+
public function getKeyName()
65+
{
66+
return 'uuid';
67+
}
6668
}
6769
```
6870

src/HasUuidPrimaryKey.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public function getIncrementing()
1313
{
1414
return false;
1515
}
16+
17+
public function resolveRouteBinding($value)
18+
{
19+
return $this->withUuid($value)->first();
20+
}
1621
}

tests/Feature/CreatesModel.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Spatie\BinaryUuid\Test\Feature;
4+
5+
use Spatie\BinaryUuid\Test\TestModel;
6+
7+
trait CreatesModel
8+
{
9+
private function createModel(string $uuid, $relationUuid = null): TestModel
10+
{
11+
$model = new TestModel();
12+
13+
$model->uuid_text = $uuid;
14+
15+
if ($relationUuid) {
16+
$model->relation_uuid = TestModel::encodeUuid($relationUuid);
17+
}
18+
19+
$model->save();
20+
21+
return $model;
22+
}
23+
}

tests/Feature/HasBinaryUuidTest.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22

3-
namespace Spatie\BinaryUuid\Test\Unit;
3+
namespace Spatie\BinaryUuid\Test\Feature;
44

55
use Ramsey\Uuid\Uuid;
66
use Spatie\BinaryUuid\Test\TestCase;
77
use Spatie\BinaryUuid\Test\TestModel;
88

99
class HasBinaryUuidTest extends TestCase
1010
{
11+
use CreatesModel;
12+
1113
/** @test */
1214
public function it_generates_the_uuid_on_save()
1315
{
@@ -153,19 +155,4 @@ public function it_serialises_the_model_correctly_with_json_encode()
153155
$this->assertContains($model->uuid_text, $json);
154156
$this->assertNotContains($model->uuid, $json);
155157
}
156-
157-
private function createModel(string $uuid, $relationUuid = null): TestModel
158-
{
159-
$model = new TestModel();
160-
161-
$model->uuid_text = $uuid;
162-
163-
if ($relationUuid) {
164-
$model->relation_uuid = TestModel::encodeUuid($relationUuid);
165-
}
166-
167-
$model->save();
168-
169-
return $model;
170-
}
171158
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Spatie\BinaryUuid\Test\Feature;
4+
5+
use Ramsey\Uuid\Uuid;
6+
use Spatie\BinaryUuid\Test\TestCase;
7+
use Spatie\BinaryUuid\Test\TestModel;
8+
use Illuminate\Routing\Middleware\SubstituteBindings;
9+
10+
class HasUuidPrimaryKeyTest extends TestCase
11+
{
12+
use CreatesModel;
13+
14+
/** @test */
15+
public function it_resolves_route_binding()
16+
{
17+
$uuid = Uuid::uuid1();
18+
$this->createModel($uuid);
19+
20+
$resolvedModel = (new TestModel())->resolveRouteBinding($uuid);
21+
22+
$this->assertEquals((string) $uuid, $resolvedModel->uuid_text);
23+
}
24+
25+
/** @test */
26+
public function laravel_resolves_route_binding_correctly()
27+
{
28+
$uuid = Uuid::uuid1();
29+
$this->createModel($uuid);
30+
31+
app('router')
32+
->middleware(SubstituteBindings::class)
33+
->group(function () {
34+
app('router')->get('uuid-test/{model}', function (TestModel $model) {
35+
return $model;
36+
});
37+
});
38+
39+
$this->get("uuid-test/{$uuid->toString()}")
40+
->assertJson([
41+
'uuid' => $uuid,
42+
]);
43+
}
44+
}

0 commit comments

Comments
 (0)