{"id":15937201,"url":"https://github.com/rawilk/human-keys","last_synced_at":"2025-07-18T23:05:11.404Z","repository":{"id":164103046,"uuid":"637833664","full_name":"rawilk/human-keys","owner":"rawilk","description":"Use stripe-like keys for your models.","archived":false,"fork":false,"pushed_at":"2024-07-08T13:15:01.000Z","size":81,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-29T13:55:53.495Z","etag":null,"topics":["human-readable-keys","ksuid","snowflake"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rawilk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"rawilk"}},"created_at":"2023-05-08T14:01:59.000Z","updated_at":"2024-05-31T21:12:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"63c65c1e-ccce-49d2-b8c5-6ac4a2d192e2","html_url":"https://github.com/rawilk/human-keys","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":"rawilk/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rawilk%2Fhuman-keys","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rawilk%2Fhuman-keys/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rawilk%2Fhuman-keys/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rawilk%2Fhuman-keys/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rawilk","download_url":"https://codeload.github.com/rawilk/human-keys/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245418112,"owners_count":20612061,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["human-readable-keys","ksuid","snowflake"],"created_at":"2024-10-07T05:01:18.570Z","updated_at":"2025-03-25T07:30:43.112Z","avatar_url":"https://github.com/rawilk.png","language":"PHP","readme":"# human-keys\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/rawilk/human-keys.svg?style=flat-square)](https://packagist.org/packages/rawilk/human-keys)\n![Tests](https://github.com/rawilk/human-keys/workflows/Tests/badge.svg?style=flat-square)\n[![Total Downloads](https://img.shields.io/packagist/dt/rawilk/human-keys.svg?style=flat-square)](https://packagist.org/packages/rawilk/human-keys)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/rawilk/human-keys?style=flat-square)](https://packagist.org/packages/rawilk/human-keys)\n[![License](https://img.shields.io/github/license/rawilk/human-keys?style=flat-square)](https://github.com/rawilk/human-keys/blob/main/LICENSE.md)\n\n![social image](https://banners.beyondco.de/Human%20Keys.png?theme=light\u0026packageManager=composer+require\u0026packageName=rawilk%2Fhuman-keys\u0026pattern=architect\u0026style=style_1\u0026description=Use+Stripe-like+keys+for+your+models.\u0026md=1\u0026showWatermark=0\u0026fontSize=100px\u0026images=key)\n\nHuman Keys offers an alternative to using UUIDs for your Laravel Models. By default, it generates ksuids, similar to what Stripe uses for their resources.\nKsuids are human-readable and sortable.\n\nExample:\n\n-   `pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR` for `Models/Post.php`\n-   `usr_p6UEyCc8D8ecLijAI5zVwOTP3D0` for `Models/User.php`\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require rawilk/human-keys\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"human-keys-config\"\n```\n\nIf you are using the `KsuidGenerator` (which is the default), you will need to install the `tuupola/ksuid` package:\n\n```bash\ncomposer require tuupola/ksuid\n```\n\nSee [Using a Different Generator](#using-a-different-generator) for more information.\n\nYou can view the default configuration here: https://github.com/rawilk/human-keys/blob/main/config/human-keys.php\n\n## Usage\n\nTo get started, use the `HasHumanKey` trait on your model:\n\n```php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Rawilk\\HumanKeys\\Concerns\\HasHumanKey;\n\nclass Post extends Model\n{\n    use HasHumanKey;\n}\n```\n\n### Using a Different Generator\n\nBy default, the package is configured to use the `KsuidGenerator`, however you may define a custom generator to use under the `generator` key in the config file.\n\nBelow you will find out how to use the different generators that are included with this package, and how to create your own.\n\n#### KsuidGenerator\n\nThis is the default generator that is used. You will need to install the `tuupola/ksuid` package to use it.\nThis generator will generate something like this: `pos_2JvL8Gv5mirjbIVAlSRFrC8EaWR`.\n\n```bash\ncomposer require tuupola/ksuid\n```\n\n#### SnowflakeGenerator\n\nThis generator generates IDs based on the Snowflake Algorithm announced by Twitter. You will need to install the `godruoyi/php-snowflake` package to use it.\nThis generator will generate something like this: `pos_451734027389370636`.\n\n```bash\ncomposer require godruoyi/php-snowflake\n```\n\n#### UuidGenerator\n\nAs of `v1.1.0`, you can use the `UuidGenerator`, which generates ids using Laravel's `Str::uuid()` helper. Ids generated with this generator will look like: `pos_b8a34e34553a41b885ae218ae81abd42`. The only requirement for this generator is to register it in the config file; there are no external dependencies that are required for it.\n\n```php\n// config/human-keys.php\n\n'generator' =\u003e \\Rawilk\\HumanKeys\\Generators\\UuidGenerator::class,\n```\n\n#### Custom Generator\n\nYou may define your own generator by implementing the `Rawilk\\HumanKeys\\Contracts\\KeyGenerator` contract. From the generator, you may return an ID based on your application's requirements.\n\n```php\nnamespace App\\Generators;\n\nuse Rawilk\\HumanKeys\\Contracts\\KeyGenerator;\n\nclass CustomGenerator implements KeyGenerator\n{\n    public function generate(?string $prefix = null): string\n    {\n        // Generate your ID here...\n    }\n}\n```\n\nThen, in the config file, you may specify your generator:\n\n```php\n'generator' =\u003e App\\Generators\\CustomGenerator::class,\n```\n\n### Overriding the Key Prefix\n\nBy default, the key the first 3 characters of the model's class name. You may override this by defining a `humanKeyPrefix` method on your model:\n\n```php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Rawilk\\HumanKeys\\Concerns\\HasHumanKey;\n\nclass Post extends Model\n{\n    use HasHumanKey;\n\n    public static function humanKeyPrefix() : string\n    {\n        // You should omit an underscore at the end of the prefix, as it will be added automatically\n        // by the generator.\n        return 'custom_prefix';\n    }\n}\n```\n\n### Using it For Other Columns\n\nBy default, the `HasHumanKey` trait will generate an ID for your model's primary key column. This may not be what you want, however.\nIn your model, you may override the `humanKeys` method and return a listing of the columns that should be generated for.\n\n```php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Rawilk\\HumanKeys\\Concerns\\HasHumanKey;\n\nclass Post extends Model\n{\n    use HasHumanKey;\n\n    public function humanKeys(): array\n    {\n        return ['human_key'];\n    }\n}\n```\n\nNow the `human_key` column will be generated for instead of the primary key. This is useful if your model is already using auto-incrementing IDs or\nif you are using `UUID` or `ULID` for your primary keys. The `HasHumanKey` trait is fully compatible with Laravel's `HasUuids` or `HasUlids` traits.\n\nIf you really need to, you may even override the `newHumanKey` method on your model to generate a custom ID in a way of your choosing, however in most cases\nthis shouldn't be necessary.\n\n## Scripts\n\n### Setup\n\nFor convenience, you can run the setup bin script for easy installation for local development.\n\n```bash\n./bin/setup.sh\n```\n\n### Formatting\n\nAlthough formatting is done automatically via workflow, you can format php code locally before committing with a composer script:\n\n```bash\ncomposer format\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security\n\nPlease review [my security policy](.github/SECURITY.md) on how to report security vulnerabilities.\n\n## Alternatives\n\nThis package is very similar to the [laravel-human-keys](https://github.com/oneduo/laravel-human-keys) package by [oneduo](https://github.com/oneduo), however I created my own version because I wanted\nmore flexibility in which columns IDs are generated for.\n\nOther alternatives include:\n\n-   [spatie/laravel-prefixed-ids](https://github.com/spatie/laravel-prefixed-ids)\n\n## Credits\n\n-   [Randall Wilk](https://github.com/rawilk)\n-   [All Contributors](../../contributors)\n-   [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake)\n-   [tuupola/ksuid](https://github.com/tuupola/ksuid)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/rawilk"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frawilk%2Fhuman-keys","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frawilk%2Fhuman-keys","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frawilk%2Fhuman-keys/lists"}