{"id":17160835,"url":"https://github.com/korridor/laravel-model-validation-rules","last_synced_at":"2025-04-06T09:08:21.403Z","repository":{"id":35114432,"uuid":"208495858","full_name":"korridor/laravel-model-validation-rules","owner":"korridor","description":"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.","archived":false,"fork":false,"pushed_at":"2025-02-27T16:40:24.000Z","size":85,"stargazers_count":28,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-30T08:08:43.959Z","etag":null,"topics":["eloquent","laravel","laravel-validation","laravel-validation-rules","model","softdelete","validation"],"latest_commit_sha":null,"homepage":"","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/korridor.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-14T19:54:23.000Z","updated_at":"2025-02-27T16:07:20.000Z","dependencies_parsed_at":"2024-10-31T05:03:47.040Z","dependency_job_id":"1a3e3916-9c3a-4139-93f6-b4f4eb151326","html_url":"https://github.com/korridor/laravel-model-validation-rules","commit_stats":{"total_commits":42,"total_committers":4,"mean_commits":10.5,"dds":0.1428571428571429,"last_synced_commit":"e7c27b976eefd383de10a5195e8c986815ac43a4"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-model-validation-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-model-validation-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-model-validation-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/korridor%2Flaravel-model-validation-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/korridor","download_url":"https://codeload.github.com/korridor/laravel-model-validation-rules/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457801,"owners_count":20941906,"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","laravel","laravel-validation","laravel-validation-rules","model","softdelete","validation"],"created_at":"2024-10-14T22:26:12.147Z","updated_at":"2025-04-06T09:08:21.389Z","avatar_url":"https://github.com/korridor.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel model validation rules\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/korridor/laravel-model-validation-rules?style=flat-square)](https://packagist.org/packages/korridor/laravel-model-validation-rules)\n[![License](https://img.shields.io/packagist/l/korridor/laravel-model-validation-rules?style=flat-square)](license.md)\n[![Supported PHP versions](https://img.shields.io/packagist/php-v/korridor/laravel-model-validation-rules?style=flat-square)](https://packagist.org/packages/korridor/laravel-model-validation-rules)\n[![GitHub Workflow Lint](https://img.shields.io/github/actions/workflow/status/korridor/laravel-model-validation-rules/lint.yml?label=lint\u0026style=flat-square)](https://github.com/korridor/laravel-model-validation-rules/actions/workflows/lint.yml)\n[![GitHub Workflow Tests](https://img.shields.io/github/actions/workflow/status/korridor/laravel-model-validation-rules/unittests.yml?label=tests\u0026style=flat-square)](https://github.com/korridor/laravel-model-validation-rules/actions/workflows/unittests.yml)\n[![Codecov](https://img.shields.io/codecov/c/github/korridor/laravel-model-validation-rules?style=flat-square)](https://codecov.io/gh/korridor/laravel-model-validation-rules)\n\nThis package is an alternative to the Laravel built-in validation rules `exists` and `unique`.\nIt uses Eloquent models instead of directly querying the database.\n\n**Advantages**\n - The rule can be easily extended with the Eloquent builder. (scopes etc.)\n - Soft deletes are working out of the box.\n - Logic implemented into the models work in the validation as well. (multi tenancy system, etc.)\n\n\u003e [!NOTE]\n\u003e Check out **solidtime - The modern Open Source Time-Tracker** at [solidtime.io](https://www.solidtime.io)\n\n## Installation\n\nYou can install the package via composer with following command:\n\n```bash\ncomposer require korridor/laravel-model-validation-rules\n```\n\nIf you want to use this package with older Laravel/PHP version please install the 2.1.* version.\n\n```bash\ncomposer require korridor/laravel-model-validation-rules \"^2.1\"\n```\n\n### Requirements\n\nThis package is tested for the following Laravel and PHP versions:\n\n - 12.* (PHP 8.2, 8.3, 8.4)\n - 11.* (PHP 8.2, 8.3, 8.4)\n - 10.* (PHP 8.1, 8.2, 8.3)\n\n## Usage examples\n\n**PostStoreRequest**\n\n```php\nuse Korridor\\LaravelModelValidationRules\\Rules\\UniqueEloquent;\nuse Korridor\\LaravelModelValidationRules\\Rules\\ExistsEloquent;\n// ...\npublic function rules(): array\n{\n    $postId = $this-\u003epost-\u003eid;\n    \n    return [\n        'username' =\u003e [new UniqueEloquent(User::class, 'username')],\n        'title' =\u003e ['string'],\n        'content' =\u003e ['string'],\n        'comments.*.id' =\u003e [\n            'nullable',\n            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {\n                return $builder-\u003ewhere('post_id', $postId);\n            }),\n        ],\n        'comments.*.content' =\u003e ['string']\n    ];\n}\n```\n\n**PostUpdateRequest**\n\n```php\nuse Korridor\\LaravelModelValidationRules\\Rules\\UniqueEloquent;\nuse Korridor\\LaravelModelValidationRules\\Rules\\ExistsEloquent;\n// ...\npublic function rules(): array\n{\n    $postId = $this-\u003epost-\u003eid;\n    \n    return [\n        'id' =\u003e [new ExistsEloquent(Post::class)],\n        'username' =\u003e [(new UniqueEloquent(User::class, 'username'))-\u003eignore($postId)],\n        'title' =\u003e ['string'],\n        'content' =\u003e ['string'],\n        'comments.*.id' =\u003e [\n            'nullable',\n            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {\n                return $builder-\u003ewhere('post_id', $postId);\n            }),\n        ],\n        'comments.*.content' =\u003e ['string']\n    ];\n}\n```\n\n### Custom validation message\n\nIf you want to change the validation message for one specific case, you can use the `withMessage(...)` function to add a custom validation message.\nWith `withCustomTranslation(...)` you can set a custom translation key for the validation message.\nAs 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.\n\n```php\nuse Korridor\\LaravelModelValidationRules\\Rules\\UniqueEloquent;\nuse Korridor\\LaravelModelValidationRules\\Rules\\ExistsEloquent;\n// ...\npublic function rules(): array\n{\n    $postId = $this-\u003epost-\u003eid;\n    \n    return [\n        'id' =\u003e [(new ExistsEloquent(Post::class))-\u003ewithMessage('The ID already exists.')],\n        'username' =\u003e [\n            (new UniqueEloquent(User::class, 'username'))\n                -\u003eignore($postId)\n                -\u003ewithCustomTranslation('validation.custom.username.unique_eloquent')\n        ],\n        'title' =\u003e ['string'],\n        'content' =\u003e ['string'],\n        'comments.*.id' =\u003e [\n            'nullable',\n            new ExistsEloquent(Comment::class, null, function (Builder $builder) use ($postId) {\n                return $builder-\u003ewhere('post_id', $postId);\n            }),\n        ],\n        'comments.*.content' =\u003e ['string']\n    ];\n}\n```\n\n### Customize default validation message\n\nIf you want to customize the translations of the default validation errors you can publish the translations\nof the package to the `resources/lang/vendor/modelValidationRules` folder.\n\n```bash\nphp artisan vendor:publish --provider=\"Korridor\\LaravelModelValidationRules\\ModelValidationServiceProvider\"\n```\n\nYou can use the following attributes in the validation message:\n\n - `attribute`\n - `model`\n - `value`\n\n```php\nreturn [\n    'exists_model' =\u003e 'A :model with the :attribute \":value\" does not exist.',\n    'unique_model' =\u003e 'A :model with the :attribute \":value\" already exists.',\n];\n```\n\nExample outputs would be:\n\n - `A user with the id \"2\" does not exist.`\n - `A user with the id \"2\" already exists.`\n\n## Contributing\n\nI am open for suggestions and contributions. Just create an issue or a pull request.\n\n### Local docker environment\n\nThe `docker` folder contains a local docker environment for development.\nThe docker workspace has composer and xdebug installed.\n\n```bash\ndocker-compose run workspace bash\n```\n\n### Testing\n\nThe `composer test` command runs all tests with [phpunit](https://phpunit.de/).\nThe `composer test-coverage` command runs all tests with phpunit and creates a coverage report into the `coverage` folder.\n\n### Codeformatting/Linting\n\nThe `composer fix` command formats the code with [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).\nThe `composer lint` command checks the code with [phpcs](https://github.com/squizlabs/PHP_CodeSniffer).\n\n## Credits\n\nThe structure of the repository and the TestClass is inspired by the \nproject [laravel-validation-rules](https://github.com/spatie/laravel-validation-rules) by [spatie](https://github.com/spatie).\n\n## License\n\nThis package is licensed under the MIT License (MIT). Please see [license file](license.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-model-validation-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkorridor%2Flaravel-model-validation-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkorridor%2Flaravel-model-validation-rules/lists"}