{"id":17335765,"url":"https://github.com/matt-daneshvar/eloquent-hashids","last_synced_at":"2025-04-14T17:31:05.029Z","repository":{"id":62525140,"uuid":"193316778","full_name":"matt-daneshvar/eloquent-hashids","owner":"matt-daneshvar","description":"Automatically generate and persist Hashids for newly created Eloquent models.","archived":false,"fork":false,"pushed_at":"2021-02-14T09:32:54.000Z","size":22,"stargazers_count":17,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T06:12:01.842Z","etag":null,"topics":["eloquent","hashids","laravel"],"latest_commit_sha":null,"homepage":null,"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/matt-daneshvar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-23T07:02:21.000Z","updated_at":"2022-04-05T12:34:29.000Z","dependencies_parsed_at":"2022-11-02T10:31:32.156Z","dependency_job_id":null,"html_url":"https://github.com/matt-daneshvar/eloquent-hashids","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-daneshvar%2Feloquent-hashids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-daneshvar%2Feloquent-hashids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-daneshvar%2Feloquent-hashids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-daneshvar%2Feloquent-hashids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matt-daneshvar","download_url":"https://codeload.github.com/matt-daneshvar/eloquent-hashids/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586220,"owners_count":21128998,"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","hashids","laravel"],"created_at":"2024-10-15T15:12:05.296Z","updated_at":"2025-04-14T17:31:04.753Z","avatar_url":"https://github.com/matt-daneshvar.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Eloquent Hashids for Laravel\n[![Build Status](https://travis-ci.org/matt-daneshvar/eloquent-hashids.svg?branch=master)](https://travis-ci.org/matt-daneshvar/eloquent-hashids)\n![GitHub](https://img.shields.io/github/license/matt-daneshvar/eloquent-hashids.svg)\n\nAutomatically persist Hashids on your newly created Eloquent models \nusing Ivan Akimov's [Hashids library](https://github.com/ivanakimov/hashids.php).\n\nThis can be useful when you need to generate unique alphanumeric (or any other character) combinations \nto represent your models.\n\n## Installation\n\nRequire the package using composer.\n\n```bash\ncomposer require matt-daneshvar/eloquent-hashids\n```\n\n## Usage\nAdd a *nullable* `hashid` column to your database table in your migrations. \n```php\n$table-\u003estring('hashid')-\u003enullable();\n```\n\nUse the `Hashid` trait to automatically generate and persist Hashids for your new models.\nOptionally use `HashidRouting` to set your model to use the `hashid` column for \nLaravel's [Route Model Binding](https://laravel.com/docs/routing#route-model-binding). \n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse MattDaneshvar\\EloquentHashids\\Hashid;\nuse MattDaneshvar\\EloquentHashids\\HashidRouting;\n\nclass Receipt extends Model\n{\n    use Hashid, HashidRouting;\n}\n```\n\n### Customizing Hashid generation\nWhile the package attempts to use sensible defaults to minimize configuration out of the box, \nyou're free to adjust the Hashid generation behaviour using static properties on your model definition.\n```php\nclass Receipt extends Model\n{\n    use Hashid;\n    \n    /**\n     * The column used to store Hashid.\n     *\n     * @var array\n     */\n    protected static $hashidColumn = 'hashid';\n    \n    /**\n     * The minimum length of the generated Hashids.\n     *\n     * @var array\n     */\n    protected static $hashidMinLength = 8;\n    \n    /**\n     * The whitelist of characters used inside the generated Hashids.\n     *\n     * @var array\n     */\n    protected static $hashidChars = 'abcdefghijklmnopqrstuvwxyz1234567890';\n    \n    /**\n     * The salt for generating Hashids.\n     *\n     * @var array\n     */\n    protected static $hashidSalt = 'your unique salt';\n    \n    /**\n     * The attribute encoded to generate the Hashid.\n     *\n     * @var array\n     */\n    protected static $hashidKey = 'id';\n}\n```\n\n### Changing the Hashid column\nTo customize the hashid column, set your own custom `$hashidColumn` value on your model.\n```php\nclass Receipt extends Model\n{\n    use Hashid;\n    \n    protected static $hashidColumn = 'uid';\n}\n```\n\n### Changing the salt\nEach model's table name is by default used as the salt for generating Hashids.\nWith that, models of separate classes that share the same IDs \n(e.g. a `Task` model with ID of 1 and a `Receipt` model also with ID of 1) would each have different Hashids.\nYou may change this behaviour and override the salt by specifying the `$hashidSlat` on your model.\n ```php\n class Receipt extends Model\n {\n     use Hashid;\n     \n     protected static $hashidSalt = 'salt and pepper';\n }\n ```\n\n### Creating your own Hashids instance\nTo fully customize the behaviour of the underlying Hashids library, \nyou may also define your own `Hashids` instance in your model's boot method. \nNote that your Hashids instance would take precedence over \nall other customizations, and therefore all the rest of the static Hashid properties on your model \n(i.e. `$hashidMinLength`, `$hashidChars`, etc.)\nwould be ignored once you specify your own `Hashids` instance. \n```php\nclass Receipt extends Model\n{\n    public static function boot()\n    {\n        parent::boot();\n    \n        static::$hashidsInstance = new Hashids('salt and pepper', 5);\n    }\n}\n```\n\n### Using the HashidRouting trait\nA common use case of Hashids with Eloquent models \nis to use short URLs using the generated Hashids as identifiers.\n\nFor example you may wish to represent your app's receipts using their Hashid values:\n```\nhttps://example.com/receipts/2ov7j3o3\n```\ninstead of their IDs:\n```\nhttps://example.com/receipts/4\n```\n\nFor more convenience this package comes with a `HashidRouting` trait out of the box; once added to your model, \nthis trait will change the model's route key name to its corresponding Hashid column,\nwhich would allow you to take advantage of \nLaravel's [Route Model Binding](https://laravel.com/docs/routing#route-model-binding)\nand use the Hashid URLs:\n```php\nRoute::get('api/receipts/{receipt}', function (App\\Receipt $receipt) {\n    return $receipt-\u003etotal;\n});\n```\n\n## License\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%2Fmatt-daneshvar%2Feloquent-hashids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatt-daneshvar%2Feloquent-hashids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-daneshvar%2Feloquent-hashids/lists"}