Skip to content

Commit 3d0bb1d

Browse files
authored
Merge pull request #38 from kduma-OSS/docs/eloquent-uuidable
Docs: eloquent-uuidable — Laravel 13+, PHP 8.3+, PHP Attributes
2 parents 1b8d981 + 1d951a5 commit 3d0bb1d

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

content/3.libraries/100.php/eloquent-uuidable.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,73 @@ github: https://github.com/kduma-OSS/LV-eloquent-uuidable
1414
:u-button[Packagist]{icon="simple-icons:packagist" href="https://packagist.org/packages/kduma/eloquent-uuidable" target="_blank"}
1515
::
1616

17+
## Requirements
18+
19+
- PHP `^8.3`
20+
- Laravel `^13.0`
21+
22+
## Installation
23+
24+
```bash
25+
composer require kduma/eloquent-uuidable
26+
```
27+
1728
## Setup
18-
Install it using composer
1929

20-
composer require kduma/eloquent-uuidable
30+
Add the `Uuidable` trait to your model and create a `uuid` column in your migration:
31+
32+
```php
33+
use KDuma\Eloquent\Uuidable;
34+
35+
class User extends Model
36+
{
37+
use Uuidable;
38+
}
39+
```
40+
41+
```php
42+
$table->uuid('uuid')->unique();
43+
```
44+
45+
## Configuration
46+
47+
### New style — PHP Attribute (recommended)
48+
49+
```php
50+
use KDuma\Eloquent\Uuidable;
51+
use KDuma\Eloquent\Attributes\HasUuid;
52+
53+
#[HasUuid(field: 'public_uuid', checkDuplicates: true)]
54+
class User extends Model
55+
{
56+
use Uuidable;
57+
}
58+
```
2159

22-
## Prepare models
23-
Inside your model (not on top of file) add following lines:
60+
`HasUuid` parameters: `field` (default: `'uuid'`), `checkDuplicates` (default: `false`).
2461

25-
use \KDuma\Eloquent\Uuidable;
62+
### Old style — model properties (deprecated)
2663

27-
In database create `uuid` string field. If you use migrations, you can use following snippet:
64+
```php
65+
class User extends Model
66+
{
67+
use Uuidable;
2868

29-
$table->uuid('uuid')->unique();
69+
protected string $uuid_field = 'public_uuid'; // ⚠️ deprecated
70+
protected bool $check_for_uuid_duplicates = true; // ⚠️ deprecated
71+
}
72+
```
3073

3174
## Usage
32-
By default it generates slug on first save.
3375

34-
- `$model->regenerateUuid()` - Generate new uuid. (Remember to save it by yourself)
35-
- `Model::whereUuid($uuid)->first()` - Find by guid. (`whereUuid` is query scope)
76+
- UUID is auto-generated on `create` and `update` if field is `null`
77+
- `$model->regenerateUuid()` — manually regenerate (save afterwards)
78+
- `Model::whereUuid($uuid)` — query scope
79+
- `Model::byUuid($uuid)` — retrieve model by UUID
80+
- `$model->getUuidField()` — returns configured column name
3681

37-
## Upgrade from 1.x/2.x version of `kduma/eloquent-guidable`
82+
> **Note:** This package adds UUID as an *additional column* alongside the numeric `id`, unlike Laravel's built-in `HasUuids` which replaces the primary key.
3883
39-
Add following line to yours models to switch from using `uuid` column name to `guid` as it was used in previous versions:
84+
## Upgrade from `kduma/eloquent-guidable` (1.x / 2.x)
4085

41-
protected $uuid_field = 'guid';
86+
Use `#[HasUuid(field: 'guid')]` to keep the old column name.

0 commit comments

Comments
 (0)