Skip to content

Commit 374fbef

Browse files
committed
Improve interactions with Laravel assumptions
Adds a second trait, with functionality provided for reasons cited in [this article](https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be): - The `getIncrementing()` method is used by Eloquent to know if the IDs on the table are incrementing. Remember we are using UUIDs, so we set auto-incrementing to false. - The `getKeyType()` method just specifies that the IDs on the table should be stored as strings.
1 parent 0d1d017 commit 374fbef

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ Simply add the "\BinaryCabin\LaravelUUID\Traits\HasUUID;" trait to your model:
2020

2121
namespace App;
2222

23+
use BinaryCabin\LaravelUUID\Traits\HasUUID;
2324
use Illuminate\Database\Eloquent\Model;
2425

2526
class Project extends Model
2627
{
2728

28-
use \BinaryCabin\LaravelUUID\Traits\HasUUID;
29+
use HasUUID;
2930

3031
}
3132
```
@@ -47,3 +48,24 @@ And static find method:
4748
```php
4849
\App\Project::findByUUID('uuid')
4950
```
51+
52+
A second trait is available if you use your UUIDs as primary keys:
53+
54+
```php
55+
<?php
56+
57+
namespace App;
58+
59+
use BinaryCabin\LaravelUUID\Traits\HasUUID;
60+
use BinaryCabin\LaravelUUID\Traits\UUIDIsPrimaryKey;
61+
use Illuminate\Database\Eloquent\Model;
62+
63+
class Project extends Model
64+
{
65+
66+
use HasUUID, UUIDIsPrimaryKey;
67+
68+
}
69+
```
70+
71+
It simply tells Laravel that your primary key isn't an auto-incrementing integer, so it will treat the value correctly.

src/Traits/UUIDIsPrimaryKey.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace BinaryCabin\LaravelUUID\Traits;
4+
5+
trait UUIDIsPrimaryKey
6+
{
7+
public function getIncrementing()
8+
{
9+
return false;
10+
}
11+
12+
public function getKeyType()
13+
{
14+
return 'string';
15+
}
16+
}

0 commit comments

Comments
 (0)