Skip to content

Commit 9a1cdd3

Browse files
committed
Add CUID2 support: trait, cuid2() helper and schema macros
1 parent 74a2748 commit 9a1cdd3

22 files changed

Lines changed: 664 additions & 127 deletions

.github/workflows/main.yml

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,52 @@ name: run-tests
22

33
on:
44
push:
5-
branches: [master]
5+
branches: [main]
66
pull_request:
7-
branches: [master]
7+
branches: [main]
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.ref }}
11+
cancel-in-progress: true
812

913
jobs:
1014
test:
11-
runs-on: ${{ matrix.os }}
15+
runs-on: ubuntu-latest
1216
strategy:
13-
fail-fast: true
17+
fail-fast: false
1418
matrix:
15-
os: [ubuntu-latest, windows-latest]
16-
php: [7.4, 8.0]
17-
laravel: [8.*]
18-
stability: [prefer-lowest, prefer-stable]
19+
php: ['8.2', '8.3', '8.4', '8.5']
20+
laravel: ['12.*', '13.*']
21+
stability: [prefer-stable]
1922
include:
20-
- laravel: 8.*
21-
testbench: ^6.6
23+
- laravel: '12.*'
24+
testbench: '10.*'
25+
- laravel: '13.*'
26+
testbench: '11.*'
27+
exclude:
28+
# Laravel 13 requires PHP 8.3+
29+
- laravel: '13.*'
30+
php: '8.2'
2231

23-
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}
32+
name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}
2433

2534
steps:
2635
- name: Checkout code
27-
uses: actions/checkout@v2
36+
uses: actions/checkout@v4
2837

2938
- name: Setup PHP
3039
uses: shivammathur/setup-php@v2
3140
with:
3241
php-version: ${{ matrix.php }}
33-
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
42+
extensions: dom, curl, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, bcmath, gmp
3443
coverage: none
3544

36-
- name: Setup problem matchers
37-
run: |
38-
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
39-
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
4045
- name: Install dependencies
4146
run: |
42-
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
47+
# Don't let a security advisory on an older stable release block the CI matrix.
48+
composer config policy.advisories.block false
49+
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update
4350
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
51+
4452
- name: Execute tests
4553
run: vendor/bin/phpunit

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor
2+
/.idea
3+
composer.lock
4+
.phpunit.result.cache
5+
.phpunit.cache
6+
build

.idea/.gitignore

Lines changed: 0 additions & 10 deletions
This file was deleted.

.idea/laravel-cuid2.iml

Lines changed: 0 additions & 11 deletions
This file was deleted.

.idea/laravel-idea-personal.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

.idea/laravel-idea.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.idea/modules.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

.idea/php.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
# Changelog
22

3-
All notable changes to `laravel-cuid2` will be documented in this file
3+
All notable changes to `laravel-cuid2` will be documented in this file.
44

5-
## 1.0.0 - 201X-XX-XX
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
67

7-
- initial release
8+
## 1.0.0 - 2026-07-12
9+
10+
### Added
11+
12+
- `HasCuid2` trait that auto-populates a model's primary key with a CUID2 on create,
13+
sets `keyType` to `string` and disables auto-incrementing, and supports additional
14+
columns via `uniqueIds()`.
15+
- Global `cuid2()` helper for generating identifiers, with a configurable length.
16+
- `cuid2()` and `foreignCuid2()` schema blueprint macros for migrations.
17+
- `LaravelCuid2` facade with `generate()` and `isValid()` methods.
18+
- Publishable configuration file with a `length` option (default `24`).
19+
- Support for PHP 8.2+ and Laravel 12 and 13 (Laravel 13 requires PHP 8.3+).
20+
21+
Generation is powered by the [`visus/cuid2`](https://github.com/visus-io/php-cuid2) library.

README.md

Lines changed: 96 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,129 @@
1-
# Very short description of the package
1+
# Laravel CUID2
2+
3+
[🇬🇧 **English**](README.md) | [🇷🇺 Русский](README.ru.md)
24

35
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mcandylab/laravel-cuid2.svg?style=flat-square)](https://packagist.org/packages/mcandylab/laravel-cuid2)
46
[![Total Downloads](https://img.shields.io/packagist/dt/mcandylab/laravel-cuid2.svg?style=flat-square)](https://packagist.org/packages/mcandylab/laravel-cuid2)
5-
![GitHub Actions](https://github.com/mcandylab/laravel-cuid2/actions/workflows/main.yml/badge.svg)
7+
[![run-tests](https://github.com/mcandylab/laravel-cuid2/actions/workflows/main.yml/badge.svg)](https://github.com/mcandylab/laravel-cuid2/actions/workflows/main.yml)
68

7-
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what PSRs you support to avoid any confusion with users and contributors.
9+
Use [CUID2](https://github.com/paralleldrive/cuid2) as primary keys for your Eloquent
10+
models in Laravel. The package provides a model trait, a global `cuid2()` helper and
11+
schema macros for migrations. Generation is delegated to the
12+
[`visus/cuid2`](https://github.com/visus-io/php-cuid2) library.
813

9-
## Installation
14+
## Requirements
1015

11-
You can install the package via composer:
16+
- PHP >= 8.2
17+
- Laravel 12 or 13 (Laravel 13 requires PHP 8.3+)
18+
19+
## Installation
1220

1321
```bash
1422
composer require mcandylab/laravel-cuid2
1523
```
1624

25+
The package uses auto-discovery. Publish the config if needed:
26+
27+
```bash
28+
php artisan vendor:publish --provider="Mcandylab\LaravelCuid2\LaravelCuid2ServiceProvider" --tag="config"
29+
```
30+
1731
## Usage
1832

33+
### Model trait
34+
35+
Add the `HasCuid2` trait — the primary key will be automatically populated with a
36+
valid CUID2 when a record is created:
37+
38+
```php
39+
use Illuminate\Database\Eloquent\Model;
40+
use Mcandylab\LaravelCuid2\Concerns\HasCuid2;
41+
42+
class Post extends Model
43+
{
44+
use HasCuid2;
45+
}
46+
```
47+
48+
The trait sets `keyType = 'string'` and `incrementing = false` for you.
49+
To generate a cuid2 for more than just the primary key, override `uniqueIds()`:
50+
51+
```php
52+
public function uniqueIds(): array
53+
{
54+
return [$this->getKeyName(), 'public_id'];
55+
}
56+
```
57+
58+
### Migrations
59+
60+
The `cuid2()` and `foreignCuid2()` macros declare `char` columns of the configured length:
61+
62+
```php
63+
Schema::create('posts', function (Blueprint $table) {
64+
$table->cuid2()->primary(); // id column
65+
$table->string('title');
66+
$table->timestamps();
67+
});
68+
69+
Schema::create('comments', function (Blueprint $table) {
70+
$table->cuid2()->primary();
71+
$table->foreignCuid2('post_id')->constrained();
72+
$table->text('body');
73+
});
74+
```
75+
76+
### Helper
77+
78+
```php
79+
$id = cuid2(); // 24 characters (or config('laravel-cuid2.length'))
80+
$short = cuid2(10); // arbitrary length 4..32
81+
```
82+
83+
### Facade
84+
1985
```php
20-
// Usage description here
86+
use Mcandylab\LaravelCuid2\LaravelCuid2Facade as Cuid2;
87+
88+
Cuid2::generate(); // generate an id
89+
Cuid2::isValid($someId); // validate a string
2190
```
2291

23-
### Testing
92+
## Configuration
93+
94+
`config/laravel-cuid2.php`:
95+
96+
```php
97+
return [
98+
// Identifier length (4..32). The cuid2 standard is 24.
99+
'length' => (int) env('CUID2_LENGTH', 24),
100+
];
101+
```
102+
103+
## Testing
24104

25105
```bash
26106
composer test
27107
```
28108

29-
### Changelog
109+
## Changelog
30110

31-
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
111+
See [CHANGELOG](CHANGELOG.md).
32112

33113
## Contributing
34114

35-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
115+
See [CONTRIBUTING](CONTRIBUTING.md).
36116

37-
### Security
117+
## Security
38118

39-
If you discover any security related issues, please email an.abramov@internet.ru instead of using the issue tracker.
119+
If you discover any security related issues, please [open an issue](https://github.com/mcandylab/laravel-cuid2/issues).
40120

41121
## Credits
42122

43-
- [Andrey Abramov](https://github.com/mcandylab)
44-
- [All Contributors](../../contributors)
123+
- [Andrey Abramov](https://github.com/mcandylab)
124+
- [All Contributors](../../contributors)
125+
- [visus-io/php-cuid2](https://github.com/visus-io/php-cuid2)
45126

46127
## License
47128

48-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
49-
50-
## Laravel Package Boilerplate
51-
52-
This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).
129+
The MIT License (MIT). See [License File](LICENSE.md).

0 commit comments

Comments
 (0)