{"id":19324916,"url":"https://github.com/spatie/laravel-prefixed-ids","last_synced_at":"2025-05-15T17:08:37.892Z","repository":{"id":43093747,"uuid":"341852094","full_name":"spatie/laravel-prefixed-ids","owner":"spatie","description":"Friendly prefixed IDs for Laravel models","archived":false,"fork":false,"pushed_at":"2025-02-17T10:03:17.000Z","size":73,"stargazers_count":168,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-08T06:48:06.349Z","etag":null,"topics":["api","dx","eloquent","laravel"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie"}},"created_at":"2021-02-24T09:49:08.000Z","updated_at":"2025-04-11T21:30:32.000Z","dependencies_parsed_at":"2024-06-19T11:13:58.897Z","dependency_job_id":null,"html_url":"https://github.com/spatie/laravel-prefixed-ids","commit_stats":{"total_commits":71,"total_committers":7,"mean_commits":"10.142857142857142","dds":0.3661971830985915,"last_synced_commit":"66474d3181ed78d8288742f45fdc3dc81574a0e9"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-prefixed-ids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-prefixed-ids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-prefixed-ids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-prefixed-ids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-prefixed-ids/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384989,"owners_count":22062422,"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":["api","dx","eloquent","laravel"],"created_at":"2024-11-10T02:07:30.073Z","updated_at":"2025-05-15T17:08:32.883Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie"],"categories":[],"sub_categories":[],"readme":"# Friendly prefixed IDs for Laravel models\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-prefixed-ids.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-prefixed-ids)\n[![run-tests](https://github.com/spatie/laravel-prefixed-ids/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-prefixed-ids/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-prefixed-ids.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-prefixed-ids)\n\nPrefixing an id will help users to recognize what kind of id it is. Stripe does this by default: customer ids are prefixed with `cus`, secret keys in production are prefixed with `sk_live_`, secret keys of a testing environment with `sk_test_` [and so on...](https://gist.github.com/fnky/76f533366f75cf75802c8052b577e2a5).\n\nThis package can generate such friendly prefixed ids for Eloquent models. Here's how such generated ids could look like.\n\n```\nuser_fj39fj3lsmxlsl\ntest_token_dvklms109dls\n```\n\nThe package can retrieve the model for a given prefixed id.\n\n```php\n// on a specific model\nUser::findByPrefixedId('user_fj39fj3lsmxlsl'); // returns a User model or `null`\nUser::findByPrefixedIdOrFail('user_fj39fj3lsmxlsl'); // returns a User model or throws `NoPrefixedModelFound`\n\n// automatically determine the model of a given prefixed id\n$user = PrefixedIds::getModelClass('user_fj39fj3lsmxlsl') // returns the right model for the id or `null`;\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-prefixed-ids.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-prefixed-ids)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-prefixed-ids\n```\n\n### Preparing your models\n\nOn each model that needs a prefixed id, you should use the `Spatie\\PrefixedIds\\Models\\Concerns\\HasPrefixedId` trait.\n\n```php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Spatie\\PrefixedIds\\Models\\Concerns\\HasPrefixedId;\n\nclass YourModel extends Model\n{\n    use HasPrefixedId;\n}\n```\n\n### Preparing the database\n\nFor each model that needs a prefixed id, you'll need to write a migration to add a `prefixed_id` column to its underlying table.\n\nIf you wish to use another attribute name, you should publish the config file (see below) and set the `prefixed_id_attribute_name` config value to the attribute name of your liking.\n\n```php\nSchema::create('your_models_table', function (Blueprint $table) {\n   $table-\u003estring('prefixed_id')-\u003enullable()-\u003eunique();\n});\n```\n\n### Registering models with prefixed ids\n\nTo register your models, you should pass the desired prefix and the class name of your model to `PrefixedIds::registerModels`.\n\n```php\nSpatie\\PrefixedIds\\PrefixedIds::registerModels([\n    'your_prefix_' =\u003e YourModel::class,\n    'another_prefix' =\u003e AnotherModel::class,\n]);\n```\n\nTypically, you would put the code above in a service provider.\n\n### Publish the config file\n\nOptionally, You can publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\PrefixedIds\\PrefixedIdsServiceProvider\" --tag=\"prefixed-ids-config\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n    /*\n     * The attribute name used to store prefixed ids on a model\n     */\n    'prefixed_id_attribute_name' =\u003e 'prefixed_id',\n];\n```\n\n## Usage\n\nWhen a model is created, it will automatically have a unique, prefixed id in the `prefixed_id` attribute.\n\n```php\n$model = YourModel::create();\n$model-\u003eprefixed_id // returns a random id like `your_model_fekjlmsme39dmMS`\n```\n\n### Finding a specific model\n\nYou can find the model with a given prefix by calling `findByPrefixedId` on it.\n\n```php\nYourModel::findByPrefixedId('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel`\nYourModel::findByPrefixedId('non-existing-id'); // returns null\nYourModel::findByPrefixedIdOrFail('non-existing-id'); // throws `NoPrefixedModelFound`\n```\n\n### Finding across models\n\nYou can call `find` on `Spatie\\PrefixedIds\\PrefixedIds` to automatically get the right model for any given prefixed id.\n\n```php\n$yourModel = Spatie\\PrefixedIds\\PrefixedIds::find('your_model_fekjlmsme39dmMS'); // returns an instance of `YourModel` or `null`\n$otherModel = Spatie\\PrefixedIds\\PrefixedIds::find('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or `null`\n$otherModel = Spatie\\PrefixedIds\\PrefixedIds::findOrFail('other_model_3Fjmmfsmls'); // returns an instance of `OtherModel` or throws `NoPrefixedModelFound`\n```\n\n### Customizing the unique ID generated\n\nYou can use the function `Spatie\\PrefixedIds\\PrefixedIds::generateUniqueIdUsing()` to pass in a function to generate the unique ID.  By default the library will use `Str::uuid()` to generate the ID.\n\n```php\n// generate a unique Id with a set length\nSpatie\\PrefixedIds\\PrefixedIds::generateUniqueIdUsing(function(){\n    $length = 8;\n    return substr(md5(uniqid(mt_rand(), true)), 0, $length);\n});\n```\n\n## Using the prefixed ids in your routes\n\nTo use the prefixed ids in your routes, you'll have to add the `getRouteKeyName` method to your model. It should return the name of the attribute that holds the prefixed id.\n\n```php\npublic function getRouteKeyName()\n{\n    return 'prefixed_id';\n}\n```\n\nWith this in place a route defined as...\n\n```php\nRoute::get('/api/your-models/{yourModel}', YourModelController::class)`\n```\n\n... can be invoked with an URL like `/api/your-models/your_model_fekjlmsme39dmMS`.\n\nYou'll find more info on route model binding in [the Laravel docs](https://laravel.com/docs/master/routing#route-model-binding).\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](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\nThis package is inspired by [excid3/prefixed_ids](https://github.com/excid3/prefixed_ids)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-prefixed-ids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-prefixed-ids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-prefixed-ids/lists"}