Skip to content

Commit bd2ff8f

Browse files
authored
Merge pull request #37 from kduma-OSS/docs/eloquent-ulidable
Docs: eloquent-ulidable — Laravel 13+, PHP 8.3+, PHP Attributes
2 parents ff7ce61 + 3803654 commit bd2ff8f

1 file changed

Lines changed: 57 additions & 10 deletions

File tree

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,69 @@ github: https://github.com/kduma-OSS/LV-eloquent-ulidable
1414
:u-button[Packagist]{icon="simple-icons:packagist" href="https://packagist.org/packages/kduma/eloquent-ulidable" 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-ulidable
26+
```
27+
1728
## Setup
18-
Install it using composer
1929

20-
composer require kduma/eloquent-ulidable
30+
Add the `Ulidable` trait to your model and create a `ulid` column in your migration:
31+
32+
```php
33+
use KDuma\Eloquent\Ulidable;
34+
35+
class Post extends Model
36+
{
37+
use Ulidable;
38+
}
39+
```
2140

22-
## Prepare models
23-
Inside your model (not on top of file) add following lines:
41+
```php
42+
$table->ulid()->unique();
43+
```
2444

25-
use \KDuma\Eloquent\Ulidable;
45+
## Configuration
2646

27-
In database create `ulid` string field. If you use migrations, you can use following snippet:
47+
### New style — PHP Attribute (recommended)
2848

29-
$table->ulid()->unique();
49+
```php
50+
use KDuma\Eloquent\Ulidable;
51+
use KDuma\Eloquent\Attributes\HasUlid;
52+
53+
#[HasUlid(field: 'public_id', checkDuplicates: true)]
54+
class Post extends Model
55+
{
56+
use Ulidable;
57+
}
58+
```
59+
60+
`HasUlid` parameters: `field` (default: `'ulid'`), `checkDuplicates` (default: `false`).
61+
62+
### Old style — model properties (deprecated)
63+
64+
```php
65+
class Post extends Model
66+
{
67+
use Ulidable;
68+
69+
protected string $ulid_field = 'public_id'; // ⚠️ deprecated
70+
protected bool $check_for_ulid_duplicates = true; // ⚠️ deprecated
71+
}
72+
```
3073

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

34-
- `$model->regenerateUlid()` - Generate new ulid. (Remember to save it by yourself)
35-
- `Model::whereUlid($ulid)->first()` - Find by ulid. (`whereUlid` is query scope)
76+
- ULID is auto-generated on `create` and `update` if field is `null`
77+
- `$model->regenerateUlid()` — manually regenerate (save afterwards)
78+
- `Model::whereUlid($ulid)` — query scope
79+
- `Model::byUlid($ulid)` — retrieve model by ULID
80+
- `$model->getUlidField()` — returns configured column name
81+
82+
> **Note:** This package adds ULID as an *additional column* alongside the numeric `id`, unlike Laravel's built-in `HasUlids` which replaces the primary key.

0 commit comments

Comments
 (0)