PHPackages                             korridor/laravel-model-validation-rules - 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. korridor/laravel-model-validation-rules

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

korridor/laravel-model-validation-rules
=======================================

A laravel validation rule that uses eloquent to validate if a model exists

3.3.0(1y ago)29188.9k↓11.3%6[2 PRs](https://github.com/korridor/laravel-model-validation-rules/pulls)MITPHPPHP &gt;=8.1CI passing

Since Sep 14Pushed 5mo ago3 watchersCompare

[ Source](https://github.com/korridor/laravel-model-validation-rules)[ Packagist](https://packagist.org/packages/korridor/laravel-model-validation-rules)[ Docs](https://github.com/korridor/laravel-model-validation-rules)[ RSS](/packages/korridor-laravel-model-validation-rules/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

Laravel model validation rules
==============================

[](#laravel-model-validation-rules)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6670659c570d7eed88a96bdfc3cf1f5d0c71db17dfa1ad147aef754f55d94a76/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/korridor/laravel-model-validation-rules)[![License](https://camo.githubusercontent.com/c63ff9e4915f4e3ae5c101557b24525472f048f7efd2a3e71cf1c349d9adbf98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65733f7374796c653d666c61742d737175617265)](license.md)[![Supported PHP versions](https://camo.githubusercontent.com/dfe37b2b9b81a30786d977d61326f49bbf0964962732c22ed1afafebf2a3ed43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/korridor/laravel-model-validation-rules)[![GitHub Workflow Lint](https://camo.githubusercontent.com/ea47ae06bc6062bb8e6dd832fd5eb0a0f0ae27f581e246f4506c5f1c7fc7b92c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65732f6c696e742e796d6c3f6c6162656c3d6c696e74267374796c653d666c61742d737175617265)](https://github.com/korridor/laravel-model-validation-rules/actions/workflows/lint.yml)[![GitHub Workflow Tests](https://camo.githubusercontent.com/4dcf39479bb41f92cfb5d5ace63c59d7f81004839182d1cfd663dddcf22d7479/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65732f756e697474657374732e796d6c3f6c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/korridor/laravel-model-validation-rules/actions/workflows/unittests.yml)[![Codecov](https://camo.githubusercontent.com/47f1fb28076436ebe37ac322bea43d8fff2e04271655947b9cbfe2153888f341/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6b6f727269646f722f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2d72756c65733f7374796c653d666c61742d737175617265)](https://codecov.io/gh/korridor/laravel-model-validation-rules)

This package is an alternative to the Laravel built-in validation rules `exists` and `unique`. It uses Eloquent models instead of directly querying the database.

**Advantages**

- The rule can be easily extended with the Eloquent builder. (scopes etc.)
- Soft deletes are working out of the box.
- Logic implemented into the models work in the validation as well. (multi tenancy system, etc.)

Note

Check out **solidtime - The modern Open Source Time-Tracker** at [solidtime.io](https://www.solidtime.io)

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

[](#installation)

You can install the package via composer with following command:

```
composer require korridor/laravel-model-validation-rules
```

If you want to use this package with older Laravel/PHP version please install the 2.1.\* version.

```
composer require korridor/laravel-model-validation-rules "^2.1"
```

### Requirements

[](#requirements)

This package is tested for the following Laravel and PHP versions:

- 12.\* (PHP 8.2, 8.3, 8.4)
- 11.\* (PHP 8.2, 8.3, 8.4)
- 10.\* (PHP 8.1, 8.2, 8.3)

Usage examples
--------------

[](#usage-examples)

**PostStoreRequest**

```
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;

    return [
        'username' => [new UniqueEloquent(User::class, 'username')],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}
```

**PostUpdateRequest**

```
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;

    return [
        'id' => [new ExistsEloquent(Post::class)],
        'username' => [(new UniqueEloquent(User::class, 'username'))->ignore($postId)],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}
```

### Custom validation message

[](#custom-validation-message)

If you want to change the validation message for one specific case, you can use the `withMessage(...)` function to add a custom validation message. With `withCustomTranslation(...)` you can set a custom translation key for the validation message. As described in detail in the next example ([Customize default validation message](#customize-default-validation-message)), it is possible to use `:attribute`, `:model` and `:value` in the translation.

```
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;
// ...
public function rules(): array
{
    $postId = $this->post->id;

    return [
        'id' => [(new ExistsEloquent(Post::class))->withMessage('The ID already exists.')],
        'username' => [
            (new UniqueEloquent(User::class, 'username'))
                ->ignore($postId)
                ->withCustomTranslation('validation.custom.username.unique_eloquent')
        ],
        'title' => ['string'],
        'content' => ['string'],
        'comments.*.id' => [
            'nullable',
            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
                return $builder->where('post_id', $postId);
            }),
        ],
        'comments.*.content' => ['string']
    ];
}
```

### Customize default validation message

[](#customize-default-validation-message)

If you want to customize the translations of the default validation errors you can publish the translations of the package to the `resources/lang/vendor/modelValidationRules` folder.

```
php artisan vendor:publish --provider="Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
```

You can use the following attributes in the validation message:

- `attribute`
- `model`
- `value`

```
return [
    'exists_model' => 'A :model with the :attribute ":value" does not exist.',
    'unique_model' => 'A :model with the :attribute ":value" already exists.',
];
```

Example outputs would be:

- `A user with the id "2" does not exist.`
- `A user with the id "2" already exists.`

Contributing
------------

[](#contributing)

I am open for suggestions and contributions. Just create an issue or a pull request.

### Local docker environment

[](#local-docker-environment)

The `docker` folder contains a local docker environment for development. The docker workspace has composer and xdebug installed.

```
docker-compose run workspace bash
```

### Testing

[](#testing)

The `composer test` command runs all tests with [phpunit](https://phpunit.de/). The `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.

### Codeformatting/Linting

[](#codeformattinglinting)

The `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). The `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).

Credits
-------

[](#credits)

The structure of the repository and the TestClass is inspired by the project [laravel-validation-rules](https://github.com/spatie/laravel-validation-rules) by [spatie](https://github.com/spatie).

License
-------

[](#license)

This package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance58

Moderate activity, may be stable

Popularity45

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 83% 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 ~181 days

Recently: every ~278 days

Total

12

Last Release

445d ago

Major Versions

1.0.1 → 2.0.02019-09-18

2.1.1 → 3.0.02023-02-16

PHP version history (7 changes)1.0.0PHP 7.\*

2.0.0PHP ^7.1|^7.2|^7.3

2.0.2PHP ^7.1|^7.2|^7.3|^7.4

2.0.3PHP ^7.1.0

2.1.0PHP ^7.1.0|^8.0

2.1.1PHP ^7.1|^8.0

3.0.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/728181ca75a1f175821f8adf7526047da19253c88ec012b236031f4fab1c5cf0?d=identicon)[korridor](/maintainers/korridor)

---

Top Contributors

[![korridor](https://avatars.githubusercontent.com/u/26689068?v=4)](https://github.com/korridor "korridor (39 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![intrepidws](https://avatars.githubusercontent.com/u/125735?v=4)](https://github.com/intrepidws "intrepidws (1 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (1 commits)")[![lonnylot](https://avatars.githubusercontent.com/u/656791?v=4)](https://github.com/lonnylot "lonnylot (1 commits)")

---

Tags

eloquentlaravellaravel-validationlaravel-validation-rulesmodelsoftdeletevalidationlaravelvalidationmodeleloquentruleexist

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/korridor-laravel-model-validation-rules/health.svg)

```
[![Health](https://phpackages.com/badges/korridor-laravel-model-validation-rules/health.svg)](https://phpackages.com/packages/korridor-laravel-model-validation-rules)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[watson/validating

Eloquent model validating trait.

9723.3M47](/packages/watson-validating)[dyrynda/laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models.

4802.8M8](/packages/dyrynda-laravel-model-uuid)[spiritix/lada-cache

A Redis based, automated and scalable database caching layer for Laravel

591444.8k2](/packages/spiritix-lada-cache)[wendelladriel/laravel-lift

Take your Eloquent Models to the next level

70046.8k](/packages/wendelladriel-laravel-lift)

PHPackages © 2026

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