PHPackages                             codezero/laravel-route-key-exists - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. codezero/laravel-route-key-exists

ActiveLibrary[Database &amp; ORM](/categories/database)

codezero/laravel-route-key-exists
=================================

Laravel validation rule to check if a custom route key exists.

1.1.0(8y ago)4149MITPHPPHP &gt;=7.0.0

Since Oct 25Pushed 8y ago3 watchersCompare

[ Source](https://github.com/codezero-be/laravel-route-key-exists)[ Packagist](https://packagist.org/packages/codezero/laravel-route-key-exists)[ RSS](/packages/codezero-laravel-route-key-exists/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Laravel Route Key Exists
========================

[](#laravel-route-key-exists)

![GitHub release](https://camo.githubusercontent.com/7e4cd3d11164eb073f43b8649820a52043519faed8a80f63c6d48cfad46810dd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f636f64657a65726f2d62652f6c61726176656c2d726f7574652d6b65792d6578697374732e737667)![License](https://camo.githubusercontent.com/33f5313a1d5e2d90a707dc4de2eec58090658ce8331ba9a12e594a9aa5b063ec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f64657a65726f2f6c61726176656c2d726f7574652d6b65792d6578697374732e737667)[![Build Status](https://camo.githubusercontent.com/62cc7c3ac56599d9788287729830636e393235237f68d45d7c1d03836201cd75/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f64657a65726f2d62652f6c61726176656c2d726f7574652d6b65792d6578697374732f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/c4ea05431baa87b76ec62c13ac52e4e8f62eea0e7664fbe77c8de2e8b9dcd956/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f64657a65726f2d62652f6c61726176656c2d726f7574652d6b65792d6578697374732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c9aa01917eb3f5600557f2471440546cbe0eabb69b6289066c73f8b46eca160d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f64657a65726f2d62652f6c61726176656c2d726f7574652d6b65792d6578697374732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/codezero-be/laravel-route-key-exists/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/db9d7b6d4649a4c10c1f54925d0fab40af979cecb8c14cb15861a6881908b1f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64657a65726f2f6c61726176656c2d726f7574652d6b65792d6578697374732e737667)](https://packagist.org/packages/codezero/laravel-route-key-exists)

#### Laravel validation rule to check if a custom route key exists.

[](#laravel-validation-rule-to-check-if-a-custom-route-key-exists)

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.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.0
- [Laravel](https://laravel.com/) &gt;= 5.5

Installation
------------

[](#installation)

Require the package via Composer:

```
composer require codezero/laravel-route-key-exists

```

Some Background Info
--------------------

[](#some-background-info)

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:

```
public function getRouteKeyName()
{
    return 'id';
}
```

This also works when you are using a custom or computed route key in your model:

```
public function getRouteKey()
{
    // "encode" the route key
    return "foo-{$this->id}";
}

public function resolveRouteBinding($value)
{
    // "decode" the route key
    $id = (int) str_replace('foo-', '', $value);

    // resolve from the database
    return $this->where('id', $id)->first();
}
```

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.

Usage
-----

[](#usage)

Let's say you have a model with an ID of `1`, but `getRouteKey()` returns the encoded value of `1234`.

In your validation rules, pass your model's class name to `\CodeZero\RouteKeyExists\RouteKeyExists`:

```
request()->validate([
    'model_id' => RouteKeyExists::model(Model::class),
]);
```

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.

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:

```
request()->validate([
    'model_id' => RouteKeyExists::model(Model::class)->replace(),
]);

$id = request('model_id'); // actual ID
```

If your form uses a different attribute name than your model or database, you can replace the ID and the attribute name in the process.

```
request()->validate([
    'model' => RouteKeyExists::model(Model::class)->replace('model_id'),
]);

$id = request('model_id'); // actual ID
//$id = request('model'); // null
```

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:

```
request()->validate([
    'model_id' => RouteKeyExists::model(Model::class)->add('actual_id'),
]);

$id = request('actual_id'); // actual ID
$key = request('model_id'); // route key
```

Beware that attributes that are dynamically added to the request will not be included in the array that is returned from `request()->validate()`. You can access those via `request('attribute_name')`.

Useful Packages
---------------

[](#useful-packages)

- This rule works perfectly with the [`laravel-optimus`](https://github.com/cybercog/laravel-optimus) model trait.

Testing
-------

[](#testing)

```
vendor/bin/phpunit

```

Security
--------

[](#security)

If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.

Changelog
---------

[](#changelog)

See a list of important changes in the [changelog](https://github.com/codezero-be/laravel-route-key-exists/blob/master/CHANGELOG.md).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/codezero-be/laravel-route-key-exists/blob/master/LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~6 days

Total

3

Last Release

3106d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e17b7a892452367dfb0e5c55bf04844a16bb781f356f50019332d4b9a476ec6?d=identicon)[codezero](/maintainers/codezero)

---

Top Contributors

[![ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?v=4)](https://github.com/ivanvermeyen "ivanvermeyen (7 commits)")

---

Tags

bindingdatabaseexistslaravelmodelrouterulevalidationlaravelmodelkeyrouteidbinding

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/codezero-laravel-route-key-exists/health.svg)

```
[![Health](https://phpackages.com/badges/codezero-laravel-route-key-exists/health.svg)](https://phpackages.com/packages/codezero-laravel-route-key-exists)
```

###  Alternatives

[shiftonelabs/laravel-cascade-deletes

Adds application level cascading deletes to Eloquent Models.

163632.1k2](/packages/shiftonelabs-laravel-cascade-deletes)[highsolutions/eloquent-sequence

A Laravel package for easy creation and management sequence support for Eloquent models with elastic configuration.

121130.3k](/packages/highsolutions-eloquent-sequence)[adamhopkinson/laravel-model-hash

A trait which automatically generates a unique hash per model instance

2318.7k](/packages/adamhopkinson-laravel-model-hash)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
