{"id":13828193,"url":"https://github.com/mtvs/eloquent-hashids","last_synced_at":"2025-12-26T23:44:50.799Z","repository":{"id":35052417,"uuid":"197899495","full_name":"mtvs/eloquent-hashids","owner":"mtvs","description":"On-the-fly hashids for Laravel Eloquent models. (🍰 Easy \u0026 ⚡ Fast)","archived":false,"fork":false,"pushed_at":"2024-05-07T16:22:09.000Z","size":43,"stargazers_count":291,"open_issues_count":7,"forks_count":21,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-10-20T18:12:40.622Z","etag":null,"topics":["eloquent","eloquent-models","hashids","laravel","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mtvs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-07-20T08:04:33.000Z","updated_at":"2024-08-18T14:51:04.000Z","dependencies_parsed_at":"2024-01-18T05:18:33.736Z","dependency_job_id":"d87905be-a322-4d9e-8775-62a3b263c5de","html_url":"https://github.com/mtvs/eloquent-hashids","commit_stats":{"total_commits":59,"total_committers":6,"mean_commits":9.833333333333334,"dds":0.1694915254237288,"last_synced_commit":"41dc9f554c18e4ff8e049fcd43abcf3fec0b2eb8"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-hashids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-hashids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-hashids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtvs%2Feloquent-hashids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtvs","download_url":"https://codeload.github.com/mtvs/eloquent-hashids/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225486558,"owners_count":17481926,"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":["eloquent","eloquent-models","hashids","laravel","php"],"created_at":"2024-08-04T09:02:36.014Z","updated_at":"2025-12-26T23:44:50.792Z","avatar_url":"https://github.com/mtvs.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"\u003e Using hashids instead of integer ids in urls and list items can be more\nappealing and clever. For more information visit [hashids.org](https://hashids.org/).\n\n# Eloquent-Hashids ![Build Status](https://github.com/mtvs/eloquent-hashids/actions/workflows/build.yml/badge.svg)\n\nThis adds hashids to Laravel Eloquent models by encoding/decoding them on the fly\nrather than persisting them in the database. So no need for another database column\nand also higher performance by using primary keys in queries.\n\nFeatures include:\n\n* Generating hashids for models\n* Resloving hashids to models\n* Ability to customize hashid settings for each model\n* Route binding with hashids (optional)\n\n## Installation\n\nInstall the package with Composer:\n\n```sh\n\n$ composer require mtvs/eloquent-hashids\n\n```\n\nAlso, publish the vendor config files to your application (necessary for the dependencies):\n\n```sh\n$ php artisan vendor:publish\n```\n\n## Setup\n\nBase features are provided by using `HasHashid` trait then route binding with\nhashids can be added by using `HashidRouting`.\n\n```php\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mtvs\\EloquentHashids\\HasHashid;\nuse Mtvs\\EloquentHashids\\HashidRouting;\n\nClass Item extends Model\n{\n\tuse HasHashid, HashidRouting;\n}\n\n```\n\n### Custom Hashid Settings\n\nIt's possible to customize hashids settings for each model by overwriting\n`getHashidsConnection()`. It must return the name of a connection of \n[`vinkla/hashids`](https://github.com/vinkla/laravel-hashids) that provides\nthe desired settings.\n\n## Usage\n\n### Basics\n\n```php\n\n// Generating the model hashid based on its key\n$item-\u003ehashid();\n\n// Equivalent to the above but with the attribute style\n$item-\u003ehashid;\n\n// Finding a model based on the provided hashid or\n// returning null on failure\nItem::findByHashid($hashid);\n\n// Finding a model based on the provided hashid or\n// throwing a ModelNotFoundException on failure\nItem::findByHashidOrFail($hashid);\n\n// Decoding a hashid to its equivalent id \n$item-\u003ehashidToId($hashid);\n\n// Encoding an id to its equivalent hashid\n$item-\u003eidToHashid($id);\n\n// Getting the name of the hashid connection\n$item-\u003egetHashidsConnection();\n\n```\n\n### Add the hashid to the serialized model\n\nSet it as default:\n\n```php\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mtvs\\EloquentHashids\\HasHashid;\n\nclass Item extends Model\n{\n    use HasHashid;\n    \n    /**\n     * The accessors to append to the model's array form.\n     *\n     * @var array\n     */\n    protected $appends = ['hashid'];\n}\n\n```\n\nor specify it specificly:\n\n`return $item-\u003eappend('hashid')-\u003etoJson();`\n\n\n### Implicit Route Bindings\n\nIf you want to resolve implicit route bindings for the model using its hahsid\nvalue you can use `HashidRouting` in the model.\n\n```php\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mtvs\\EloquentHashids\\HasHashid;\nuse Mtvs\\EloquentHashids\\HashidRouting;\n\nclass Item extends Model\n{\n    use HasHashid, HashidRouting;\n}\n\n```\nIt overwrites `getRouteKeyName()`, `getRouteKey()` and `resolveRouteBindingQuery()`\nto use the hashids as the route keys.\n\nIt supports the Laravel's feature for customizing the key for specific routes.\n\n```php\n\nRoute::get('/items/{item:slug}', function (Item $item) {\n    return $item;\n});\n\n```\n\n#### Customizing The Default Route Key Name\n\nIf you want to by default resolve the implicit route bindings using another \nfield you can overwrite `getRouteKeyName()` to return the field's name to the\nresolving process and `getRouteKey()` to return its value in your links.\n\n```php\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Mtvs\\EloquentHashids\\HasHashid;\nuse Mtvs\\EloquentHashids\\HashidRouting;\n\nclass Item extends Model\n{\n    use HasHashid, HashidRouting;\n\n    public function getRouteKeyName()\n    {\n        return 'slug';\n    }\n\n    public function getRouteKey()\n    {\n        return $this-\u003eslug;\n    }\n}\n\n```\n\nYou'll still be able to specify the hashid for specific routes.\n\n```php\n\nRoute::get('/items/{item:hashid}', function (Item $item) {\n    return $item;\n});\n\n```\n\n#### Supporting The Other Laravel's Implicit Route Binding Features\n\nWhen using `HashidRouting` you'll still be able to use softdeletable and child\nroute bindings.\n\n```php\n\nRoute::get('/items/{item}', function (Item $item) {\n    return $item;\n})-\u003ewithTrashed();\n\nRoute::get('/user/{user}/items/{item}', function (User $user, Item $item) {\n    return $item;\n})-\u003escopeBindings();\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtvs%2Feloquent-hashids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtvs%2Feloquent-hashids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtvs%2Feloquent-hashids/lists"}