Skip to content

Commit acaf46a

Browse files
Make UUID column configurable (#45)
Add ability to configure the uuid column name
1 parent 0042b8f commit acaf46a

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

.styleci.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
preset: laravel
2-
3-
linting: true

README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Laravel Model UUIDs
2-
## v4.0.0
2+
## v4.0.1
33

44
[![Build Status](https://travis-ci.org/michaeldyrynda/laravel-model-uuid.svg?branch=master)](https://travis-ci.org/michaeldyrynda/laravel-model-uuid)
55
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/michaeldyrynda/laravel-model-uuid/?branch=master)
@@ -39,7 +39,17 @@ class Post extends Model
3939
}
4040
```
4141

42-
It is assumed that you already have a field named `uuid` in your database, which is used to store the generated value.
42+
It is assumed that you already have a field named `uuid` in your database, which is used to store the generated value. If you wish to use a custom column name, for example if you want your primary `id` column to be a `UUID`, you can define a `uuidColumn` method in your model.
43+
44+
```php
45+
class Post extends Model
46+
{
47+
public function uuidColumn()
48+
{
49+
return 'custom_column';
50+
}
51+
}
52+
```
4353

4454
By default, this package will use UUID version 4 values, however, you are welcome to use `uuid1`, `uuid3`, `uuid4`, or `uuid5` by specifying the protected property `$uuidVersion` in your model. Should you wish to take advantage of ordered UUID (version 4) values that were introduced in Laravel 5.6, you should specify `ordered` as the `$uuidVersion` in your model.
4555

phpunit.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
convertNoticesToExceptions="true"
77
convertWarningsToExceptions="true"
88
processIsolation="false"
9-
stopOnFailure="false"
10-
syntaxCheck="false">
9+
stopOnFailure="false">
1110
<testsuites>
1211
<testsuite name="Application Test Suite">
1312
<directory>./tests/</directory>

src/GeneratesUuid.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,25 @@ public static function bootGeneratesUuid()
5656
/* @var \Illuminate\Database\Eloquent\Model|static $model */
5757
$uuid = $model->resolveUuid();
5858

59-
if (isset($model->attributes['uuid']) && ! is_null($model->attributes['uuid'])) {
59+
if (isset($model->attributes[$model->uuidColumn()]) && ! is_null($model->attributes[$model->uuidColumn()])) {
6060
/* @var \Ramsey\Uuid\Uuid $uuid */
61-
$uuid = $uuid->fromString(strtolower($model->attributes['uuid']));
61+
$uuid = $uuid->fromString(strtolower($model->attributes[$model->uuidColumn()]));
6262
}
6363

64-
$model->attributes['uuid'] = $model->hasCast('uuid') ? $uuid->getBytes() : $uuid->toString();
64+
$model->attributes[$model->uuidColumn()] = $model->hasCast('uuid') ? $uuid->getBytes() : $uuid->toString();
6565
});
6666
}
6767

68+
/**
69+
* The name of the column that should be used for the UUID.
70+
*
71+
* @return string
72+
*/
73+
public function uuidColumn()
74+
{
75+
return 'uuid';
76+
}
77+
6878
/**
6979
* Resolve a UUID instance for the configured version.
7080
*
@@ -103,11 +113,11 @@ public function resolveUuidVersion()
103113
*/
104114
public function scopeWhereUuid($query, $uuid)
105115
{
106-
if ($this->hasCast('uuid')) {
116+
if ($this->hasCast($this->uuidColumn())) {
107117
$uuid = $this->resolveUuid()->fromString($uuid)->getBytes();
108118
}
109119

110-
return $query->where('uuid', $uuid);
120+
return $query->where($this->uuidColumn(), $uuid);
111121
}
112122

113123
/**
@@ -119,7 +129,7 @@ public function scopeWhereUuid($query, $uuid)
119129
*/
120130
protected function castAttribute($key, $value)
121131
{
122-
if ($key === 'uuid' && ! is_null($value)) {
132+
if ($key === $this->uuidColumn() && ! is_null($value)) {
123133
return $this->resolveUuid()->fromBytes($value)->toString();
124134
}
125135

tests/Feature/UuidTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Tests\Fixtures\OrderedPost;
99
use Illuminate\Events\Dispatcher;
10+
use Tests\Fixtures\CustomUuidPost;
1011
use Illuminate\Container\Container;
1112
use Illuminate\Database\Capsule\Manager;
1213

@@ -23,6 +24,7 @@ public static function setupBeforeClass()
2324
$manager->schema()->create('posts', function ($table) {
2425
$table->increments('id');
2526
$table->uuid('uuid')->nullable()->default(null);
27+
$table->uuid('custom_uuid')->nullable()->default(null);
2628
$table->string('title');
2729
});
2830
}
@@ -97,4 +99,12 @@ public function it_handles_time_ordered_uuids()
9799
$this->assertInstanceOf(OrderedPost::class, $post);
98100
$this->assertNotNull($post->uuid);
99101
}
102+
103+
/** @test */
104+
public function it_allows_configurable_uuid_column_names()
105+
{
106+
$post = CustomUuidPost::create(['title' => 'test-post']);
107+
108+
$this->assertNotNull($post->custom_uuid);
109+
}
100110
}

tests/Fixtures/CustomUuidPost.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Tests\Fixtures;
4+
5+
class CustomUuidPost extends Model
6+
{
7+
public function uuidColumn()
8+
{
9+
return 'custom_uuid';
10+
}
11+
}

0 commit comments

Comments
 (0)