{"id":21976753,"url":"https://github.com/red-explosion/laravel-sqids","last_synced_at":"2025-04-28T16:41:54.257Z","repository":{"id":209544098,"uuid":"722885746","full_name":"red-explosion/laravel-sqids","owner":"red-explosion","description":"Easily generate Stripe/YouTube looking IDs for your Laravel models.","archived":false,"fork":false,"pushed_at":"2024-04-03T20:32:55.000Z","size":120,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-14T09:03:54.277Z","etag":null,"topics":["hashids","laravel","php","sqids"],"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/red-explosion.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"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}},"created_at":"2023-11-24T07:24:58.000Z","updated_at":"2024-05-05T21:37:27.389Z","dependencies_parsed_at":"2024-05-05T21:47:31.442Z","dependency_job_id":null,"html_url":"https://github.com/red-explosion/laravel-sqids","commit_stats":null,"previous_names":["red-explosion/laravel-sqids"],"tags_count":4,"template":false,"template_full_name":"red-explosion/laravel-package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/red-explosion%2Flaravel-sqids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/red-explosion%2Flaravel-sqids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/red-explosion%2Flaravel-sqids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/red-explosion%2Flaravel-sqids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/red-explosion","download_url":"https://codeload.github.com/red-explosion/laravel-sqids/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227163660,"owners_count":17740336,"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":["hashids","laravel","php","sqids"],"created_at":"2024-11-29T16:12:11.479Z","updated_at":"2024-11-29T16:12:12.054Z","avatar_url":"https://github.com/red-explosion.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg width=\"400\" src=\"./art/logo.svg\" alt=\"Laravel Sqids Logo\"\u003e\u003c/p\u003e\n\n# Laravel Sqids\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/red-explosion/laravel-sqids.svg?style=flat-square)](https://packagist.org/packages/red-explosion/laravel-sqids)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/laravel-sqids/tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/red-explosion/laravel-sqids/actions/workflows/tests.yml?query=branch:main)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/red-explosion/laravel-sqids/coding-standards.yml?label=code%20style\u0026style=flat-square)](https://github.com/red-explosion/laravel-sqids/actions/workflows/coding-standards.yml?query=branch:main)\n[![Total Downloads](https://img.shields.io/packagist/dt/red-explosion/laravel-sqids.svg?style=flat-square)](https://packagist.org/packages/red-explosion/laravel-sqids)\n\nLaravel Sqids (pronounced \"squids\") allows you to easily generate Stripe/YouTube looking IDs for your Laravel models.\nThese IDs are short and are guaranteed to be Collision free.\n\nFor more information on Sqids, we recommend checking out the official Sqids (formerly Hashids) website: [https://sqids.org](https://sqids.org).\n\n## Installation\n\nTo get started, install Laravel Sqids via the Composer package manager:\n\n```shell\ncomposer require red-explosion/laravel-sqids\n```\n\nNext, you should publish the Sqids configuration file using the `vendor:publish` artisan command. The `sqids`\nconfiguration file will be placed in your applications `config` directory:\n\n```shell\nphp artisan vendor:publish --provider=\"RedExplosion\\Sqids\\SqidsServiceProvider\"\n```\n\n## Usage\n\n### Using Sqids\n\nTo use Laravel Sqids, simply add the `RedExplosion\\Sqids\\Concerns\\HasSqids` trait to your model:\n\n```php\nuse RedExplosion\\Sqids\\Concerns\\HasSqids;\n\nclass User extends Authenticatable\n{\n    use HasSqids;\n}\n```\n\nYou will now be able to access the Sqid for the model, by calling the `sqid` attribute:\n\n```php\n$user = User::first();\n\n$sqid = $user-\u003esqid; // use_A3EyoEb2TO\n```\n\nThe result of `$sqid` will be encoded value of the models primary key along with the model prefix.\n\n\u003e [!Tip]\n\u003e Only integers can be encoded, and therefore we recommend using this package in conjunction with auto\nincrementing IDs.\n\nIf you would like to set a custom prefix for the model, you can override it by setting a `$sqidPrefix` property value\non your model like so:\n\n```php\nuse RedExplosion\\Sqids\\Concerns\\HasSqids;\n\nclass User extends Authenticatable\n{\n    use HasSqids;\n    \n    protected string $sqidPrefix = 'user';\n}\n\n$user = User::first();\n$sqid = $user-\u003esqid; // user_A3EyoEb2TO\n```\n\n### Builder Mixins\n\nLaravel Sqids provides a number of Eloquent builder mixins to make working with Sqids seamless.\n\n#### Find by Sqid\n\nTo find a model by a given Sqid, you can use the `findBySqid` method:\n\n```php\n$user = User::findBySqid('use_A3EyoEb2TO');\n```\n\nIf the model doesn't exist, `null` will be returned. However, if you would like to throw an exception, you can use\nthe `findBySqidOrFail` method instead which will throw a `ModelNotFoundException` when a model can't be found:\n\n```php\n$user = User::findBySqidOrFail('use_invalid');\n```\n\n#### Where Sqid\n\nTo add a where clause to your query, you can use the `whereSqid` method:\n\n```php\n$users = User::query()\n    -\u003ewhereSqid('use_A3EyoEb2TO')\n    -\u003eget();\n```\n\nThis will retrieve all users where the Sqid/primary key matches the given value.\n\n#### Where Sqid in\n\nTo get all models where the Sqid is in a given array, you can use the `whereSqidIn` method:\n\n```php\n$users = User::query()\n    -\u003ewhereSqidIn('id', ['use_A3EyoEb2TO'])\n    -\u003eget();\n```\n\nThis will return all users where the `id` is in the array of decoded Sqids.\n\n#### Where Sqid not in\n\nTo get all models where the Sqid is not in a given array, you can use the `whereSqidNotIn` method:\n\n```php\n$users = User::query()\n    -\u003ewhereSqidNotIn('id', ['use_A3EyoEb2TO'])\n    -\u003eget();\n```\n\nThis will return all users where the `id` is not in the array of decoded Sqids.\n\n### Route model binding\n\nLaravel Sqids supports route model binding out of the box. Simply create a route as you normally would and we'll take\ncare of the rest:\n\n```php\n// GET /users/use_A3EyoEb2TO\nRoute::get('users/{user}', function (User $user) {\n    return \"Hello $user-\u003ename\";\n});\n```\n\n### Finding a model from a Sqid\n\nOne of the most powerful features of Laravel Sqids is being able to resolve a model instance from a given Sqid. This\ncould be incredibly powerful when searching models across your application. \n\n```php\nuse RedExplosion\\Sqids\\Model;\n\n$model = Model::find('use_A3EyoEb2TO');\n```\n\nWhen we run the following, `$user` will be an instance of the `User` model for the given Sqid. If no model could be\nfound, then `null` will be returned.\n\nif you would like to throw an exception instead, you can use the `findOrFail` method which will throw an instance of\nthe `ModelNotFoundException`:\n\n```php\nuse RedExplosion\\Sqids\\Model;\n\n$model = Model::findOrFail('use_A3EyoEb2TO');\n```\n\n\u003e [!IMPORTANT]\n\u003e In order to use this feature, you must use prefixes for your Sqids.\n\n## Testing\n\n```shell\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](CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nIf you discover a security vulnerability, please send an e-mail to Ben Sherred via ben@redexplosion.co.uk. All security\nvulnerabilities will be promptly addressed.\n\n## Credits\n\n- [Ben Sherred](https://github.com/bensherred)\n- [All Contributors](../../contributors)\n\n## License\n\nLaravel Sqids is open-sourced software licensed under the [MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fred-explosion%2Flaravel-sqids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fred-explosion%2Flaravel-sqids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fred-explosion%2Flaravel-sqids/lists"}