|
1 | | -# Very short description of the package |
| 1 | +# Laravel CUID2 |
| 2 | + |
| 3 | +[🇬🇧 **English**](README.md) | [🇷🇺 Русский](README.ru.md) |
2 | 4 |
|
3 | 5 | [](https://packagist.org/packages/mcandylab/laravel-cuid2) |
4 | 6 | [](https://packagist.org/packages/mcandylab/laravel-cuid2) |
5 | | - |
| 7 | +[](https://github.com/mcandylab/laravel-cuid2/actions/workflows/main.yml) |
6 | 8 |
|
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. |
8 | 13 |
|
9 | | -## Installation |
| 14 | +## Requirements |
10 | 15 |
|
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 |
12 | 20 |
|
13 | 21 | ```bash |
14 | 22 | composer require mcandylab/laravel-cuid2 |
15 | 23 | ``` |
16 | 24 |
|
| 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 | + |
17 | 31 | ## Usage |
18 | 32 |
|
| 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 | + |
19 | 85 | ```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 |
21 | 90 | ``` |
22 | 91 |
|
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 |
24 | 104 |
|
25 | 105 | ```bash |
26 | 106 | composer test |
27 | 107 | ``` |
28 | 108 |
|
29 | | -### Changelog |
| 109 | +## Changelog |
30 | 110 |
|
31 | | -Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 111 | +See [CHANGELOG](CHANGELOG.md). |
32 | 112 |
|
33 | 113 | ## Contributing |
34 | 114 |
|
35 | | -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 115 | +See [CONTRIBUTING](CONTRIBUTING.md). |
36 | 116 |
|
37 | | -### Security |
| 117 | +## Security |
38 | 118 |
|
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). |
40 | 120 |
|
41 | 121 | ## Credits |
42 | 122 |
|
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) |
45 | 126 |
|
46 | 127 | ## License |
47 | 128 |
|
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