{"id":26085271,"url":"https://github.com/codezero-be/laravel-route-key-exists","last_synced_at":"2025-04-12T02:01:33.301Z","repository":{"id":56955968,"uuid":"108313760","full_name":"codezero-be/laravel-route-key-exists","owner":"codezero-be","description":"Laravel validation rule to check if a custom route key exists.","archived":false,"fork":false,"pushed_at":"2017-11-06T14:35:19.000Z","size":14,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-25T21:47:14.380Z","etag":null,"topics":["binding","database","exists","laravel","model","route","rule","validation"],"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/codezero-be.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-10-25T19:05:19.000Z","updated_at":"2023-10-28T13:37:50.000Z","dependencies_parsed_at":"2022-08-21T08:50:37.984Z","dependency_job_id":null,"html_url":"https://github.com/codezero-be/laravel-route-key-exists","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-route-key-exists","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-route-key-exists/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-route-key-exists/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codezero-be%2Flaravel-route-key-exists/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codezero-be","download_url":"https://codeload.github.com/codezero-be/laravel-route-key-exists/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505862,"owners_count":21115354,"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":["binding","database","exists","laravel","model","route","rule","validation"],"created_at":"2025-03-09T05:58:06.026Z","updated_at":"2025-04-12T02:01:33.266Z","avatar_url":"https://github.com/codezero-be.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Route Key Exists\n\n[![GitHub release](https://img.shields.io/github/release/codezero-be/laravel-route-key-exists.svg)]()\n[![License](https://img.shields.io/packagist/l/codezero/laravel-route-key-exists.svg)]()\n[![Build Status](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/build.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/build-status/master)\n[![Code Coverage](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)\n[![Total Downloads](https://img.shields.io/packagist/dt/codezero/laravel-route-key-exists.svg)](https://packagist.org/packages/codezero/laravel-route-key-exists)\n\n#### Laravel validation rule to check if a custom route key exists.\n\nLaravel's `exists` rule checks a database table for a column with a given value. This validation rule uses the `resolveRouteBinding()` method on a model to check if a given value exists.\n\n## Requirements\n\n-   PHP \u003e= 7.0\n-   [Laravel](https://laravel.com/) \u003e= 5.5\n\n## Installation\n\nRequire the package via Composer:\n\n```\ncomposer require codezero/laravel-route-key-exists\n```\n## Some Background Info\n\nLaravel's [implicit route model binding](https://laravel.com/docs/5.5/routing#route-model-binding) allows you to automatically resolve a model by type hinting it in a controller. Furthermore, you can change the route key that is used to query the database in your model:\n\n```php\npublic function getRouteKeyName()\n{\n    return 'id';\n}\n```\n\nThis also works when you are using a custom or computed route key in your model:\n\n```php\npublic function getRouteKey()\n{\n    // \"encode\" the route key\n    return \"foo-{$this-\u003eid}\";\n}\n\npublic function resolveRouteBinding($value)\n{\n    // \"decode\" the route key\n    $id = (int) str_replace('foo-', '', $value);\n\n    // resolve from the database\n    return $this-\u003ewhere('id', $id)-\u003efirst();\n}\n```\n\nBut what if you are sending a custom key in a POST request and you want to validate it? Unlike Laravel's [`exists`](https://laravel.com/docs/5.5/validation#rule-exists) rule, this validation rule uses the `resolveRouteBinding()` method to check if the key is valid.\n\n## Usage\n\nLet's say you have a model with an ID of `1`, but `getRouteKey()` returns the encoded value of `1234`.\n\nIn your validation rules, pass your model's class name to `\\CodeZero\\RouteKeyExists\\RouteKeyExists`:\n\n```php\nrequest()-\u003evalidate([\n    'model_id' =\u003e RouteKeyExists::model(Model::class),\n]);\n```\n\nHere, `model_id` is the encoded value, which will be resolved using the `resolveRouteBinding()` method on your model. If it can't be resolved, validation fails.\n\nPossibly, you will need the actual ID to work with when validation passes. Tack on `replace()` to the rule and `model_id` will be updated to the actual ID:\n\n```php\nrequest()-\u003evalidate([\n    'model_id' =\u003e RouteKeyExists::model(Model::class)-\u003ereplace(),\n]);\n\n$id = request('model_id'); // actual ID\n```\n\nIf your form uses a different attribute name than your model or database, you can replace the ID and the attribute name in the process.\n\n```php\nrequest()-\u003evalidate([\n    'model' =\u003e RouteKeyExists::model(Model::class)-\u003ereplace('model_id'),\n]);\n\n$id = request('model_id'); // actual ID\n//$id = request('model'); // null\n```\n\nOr maybe you want to keep the encoded ID in the request, but add the actual ID as well. Just tack on `add()` and specify an attribute name:\n\n```php\nrequest()-\u003evalidate([\n    'model_id' =\u003e RouteKeyExists::model(Model::class)-\u003eadd('actual_id'),\n]);\n\n$id = request('actual_id'); // actual ID\n$key = request('model_id'); // route key\n```\n\nBeware that attributes that are dynamically added to the request will not be included in the array that is returned from `request()-\u003evalidate()`. You can access those via `request('attribute_name')`.\n\n## Useful Packages\n\n-   This rule works perfectly with the [`laravel-optimus`](https://github.com/cybercog/laravel-optimus) model trait.\n\n## Testing\n\n```\nvendor/bin/phpunit\n```\n\n## Security\n\nIf you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.\n\n## Changelog\n\nSee a list of important changes in the [changelog](https://github.com/codezero-be/laravel-route-key-exists/blob/master/CHANGELOG.md).\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-route-key-exists/blob/master/LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodezero-be%2Flaravel-route-key-exists","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodezero-be%2Flaravel-route-key-exists","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodezero-be%2Flaravel-route-key-exists/lists"}