Skip to content

Commit f961ac5

Browse files
Added UUID column name
1 parent 47a45bd commit f961ac5

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ class Book extends Model
6767
{
6868
use BinaryUuidSupportableTrait;
6969

70-
protected $primaryKey = 'uuid';
71-
protected $fillable = ['uuid'];
70+
public $uuidColumn = 'uuid';
7271
}
7372
```
7473

@@ -92,6 +91,7 @@ use Verbanent\Uuid\AbstractModel;
9291

9392
class Lang extends AbstractModel
9493
{
94+
public $uuidColumn = 'uuid';
9595
protected $primaryKey = 'uuid';
9696
protected $fillable = ['uuid'];
9797
}

src/AbstractModel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class AbstractModel extends Model
1414
{
1515
use BinaryUuidSupportableTrait;
1616

17+
public const DEFAULT_UUID_COLUMN = 'id';
18+
1719
/**
1820
* Disable incrementing of ID column.
1921
*

src/Traits/BinaryUuidSupportableTrait.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99
use Ramsey\Uuid\Codec\OrderedTimeCodec;
1010
use Ramsey\Uuid\Uuid;
11+
use Verbanent\Uuid\AbstractModel;
1112
use Verbanent\Uuid\Exceptions\AccessedUnsetUuidPropertyException;
1213

1314
/**
@@ -61,7 +62,7 @@ public static function bootBinaryUuidSupportableTrait(): void
6162
{
6263
static::creating(
6364
function (Model $model) {
64-
$uuid = $model->getKeyName();
65+
$uuid = $model->getUuidColumn();
6566

6667
/** @var Model|BinaryUuidSupportableTrait $model */
6768
if (!isset($model->attributes[$uuid])) {
@@ -103,7 +104,7 @@ public function generateUuid(): string
103104
*/
104105
public function uuid(): string
105106
{
106-
$uuid = $this->getKeyName();
107+
$uuid = $this->getUuidColumn();
107108

108109
if (!isset($this->$uuid)) {
109110
throw new AccessedUnsetUuidPropertyException(
@@ -114,6 +115,16 @@ public function uuid(): string
114115
return Uuid::fromBytes($this->$uuid)->toString();
115116
}
116117

118+
/**
119+
* Return primary UUID column.
120+
*
121+
* @return string
122+
*/
123+
public function getUuidColumn(): string
124+
{
125+
return $this->uuidColumn ?? AbstractModel::DEFAULT_UUID_COLUMN;
126+
}
127+
117128
/**
118129
* Returns model by its UUID or fails.
119130
*
@@ -123,7 +134,7 @@ public function uuid(): string
123134
*/
124135
public static function find(string $uuid): Model
125136
{
126-
$uuidKey = app(static::class)->getKeyName();
137+
$uuidKey = app(static::class)->getUuidColumn();
127138

128139
return static::where($uuidKey, '=', Uuid::fromString($uuid)->getBytes())->firstOrFail();
129140
}

tests/Example/AbstractExampleUuidModel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ class AbstractExampleUuidModel extends AbstractModel
2525
*/
2626
public $timestamps = false;
2727

28+
/**
29+
* The UUID column name.
30+
*
31+
* @var string
32+
*/
33+
public $uuidColumn = 'uuid';
34+
2835
/**
2936
* Column name for primary key.
3037
*

0 commit comments

Comments
 (0)