|
| 1 | +# Laravel Route Key Exists |
| 2 | + |
| 3 | +[]() |
| 4 | +[]() |
| 5 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/build-status/master) |
| 6 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master) |
| 7 | +[](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master) |
| 8 | +[](https://packagist.org/packages/codezero/laravel-route-key-exists) |
| 9 | + |
| 10 | +#### Laravel validation rule to check if a custom route key exists. |
| 11 | + |
| 12 | +Laravel'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. |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- PHP >= 7.0 |
| 17 | +- [Laravel](https://laravel.com/) >= 5.5 |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Require the package via Composer: |
| 22 | + |
| 23 | +``` |
| 24 | +composer require codezero/laravel-route-key-exists |
| 25 | +``` |
| 26 | +## Some Background Info |
| 27 | + |
| 28 | +Laravel'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: |
| 29 | + |
| 30 | +```php |
| 31 | +public function getRouteKeyName() |
| 32 | +{ |
| 33 | + return 'id'; |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +This also works when you are using a custom or computed route key in your model: |
| 38 | + |
| 39 | +```php |
| 40 | +public function getRouteKey() |
| 41 | +{ |
| 42 | + // "encode" the route key |
| 43 | + return "foo-{$this->id}"; |
| 44 | +} |
| 45 | + |
| 46 | +public function resolveRouteBinding($value) |
| 47 | +{ |
| 48 | + // "decode" the route key |
| 49 | + $id = (int) str_replace('foo-', '', $value); |
| 50 | + |
| 51 | + // resolve from the database |
| 52 | + return $this->where('id', $id)->first(); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +But 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. |
| 57 | + |
| 58 | +## Usage |
| 59 | + |
| 60 | +Let's say you have a model with an ID of `1`, but `getRouteKey()` returns the encoded value of `1234`. |
| 61 | + |
| 62 | +In your validation rules, new up `\CodeZero\RouteKeyExists\RouteKeyExists` and pass the model's class name to the constructor: |
| 63 | + |
| 64 | +```php |
| 65 | +request()->validate([ |
| 66 | + 'model_id' => new RouteKeyExists(Model::class), |
| 67 | +]); |
| 68 | +``` |
| 69 | + |
| 70 | +Here, `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. |
| 71 | + |
| 72 | +Possibly, 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: |
| 73 | + |
| 74 | +```php |
| 75 | +request()->validate([ |
| 76 | + 'model_id' => (new RouteKeyExists(Model::class))->replace(), |
| 77 | +]); |
| 78 | + |
| 79 | +$id = request('model_id'); |
| 80 | +``` |
| 81 | + |
| 82 | +Or 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: |
| 83 | + |
| 84 | +```php |
| 85 | +request()->validate([ |
| 86 | + 'model_id' => (new RouteKeyExists(Model::class))->add('actual_id'), |
| 87 | +]); |
| 88 | + |
| 89 | +$id = request('actual_id'); |
| 90 | +``` |
| 91 | + |
| 92 | +Beware that `actual_id` is not included on the `$attributes` array, since it is not being validated. |
| 93 | + |
| 94 | +## Useful Packages |
| 95 | + |
| 96 | +- This rule works perfectly with the [`laravel-optimus`](https://github.com/cybercog/laravel-optimus) model trait. |
| 97 | + |
| 98 | +## Testing |
| 99 | + |
| 100 | +``` |
| 101 | +vendor/bin/phpunit |
| 102 | +``` |
| 103 | + |
| 104 | +## Security |
| 105 | + |
| 106 | +If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker. |
| 107 | + |
| 108 | +## Changelog |
| 109 | + |
| 110 | +See a list of important changes in the [changelog](https://github.com/codezero-be/laravel-route-key-exists/blob/master/CHANGELOG.md). |
| 111 | + |
| 112 | +## License |
| 113 | + |
| 114 | +The MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-route-key-exists/blob/master/LICENSE.md) for more information. |
0 commit comments