11# Laravel model validation rules
22
3+ [ ![ 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 )
4+ [ ![ License] ( https://img.shields.io/packagist/l/korridor/laravel-model-validation-rules?style=flat-square )] ( license.md )
5+ [ ![ TravisCI] ( https://img.shields.io/travis/korridor/laravel-model-validation-rules?style=flat-square )] ( https://travis-ci.org/korridor/laravel-model-validation-rules )
6+ [ ![ StyleCI] ( https://styleci.io/repos/208495858/shield )] ( https://styleci.io/repos/208495858 )
7+
8+ This package is an alternative to the Laravel built-in validation rules ` exists ` and ` unique ` .
9+ It uses Eloquent models instead of directly querying the database.
10+
11+ ** Advantages**
12+ - The rule can be easily extended with the Eloquent builder. (scopes etc.)
13+ - Softdeletes are working out of the box.
14+ - Logic implemented into the models work in the validation as well. (multi tenancy system, etc.)
15+
316## Installation
417
18+ You can install the package via composer with following command:
19+
520``` bash
621composer require korridor/laravel-model-validation-rules
722```
823
24+ ### Translation
25+
26+ If you want to customize the translations of the validation errors you can publish the translations
27+ of the package to the ` resources/lang/vendor/modelValidationRules ` folder.
28+
929``` bash
1030php artisan vendor:publish --provider=" Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
1131```
1232
13- ## Usage
33+ ### Requirements
34+
35+ This package is tested for the following Laravel versions:
36+
37+ - 6.0
38+ - 5.8
39+ - 5.7 (stable only)
40+ - 5.6 (stable only)
41+
42+ ## Usage examples
43+
44+ ** PostStoreRequest**
45+
46+ ``` php
47+ public function rules()
48+ {
49+ $postId = $this->post->id;
50+
51+ return [
52+ 'username' => [new UniqueEloquent(User::class, 'username')],
53+ 'title' => ['string'],
54+ 'content' => ['string'],
55+ 'comments.*.id' => [
56+ 'nullable',
57+ new ExistEloquent(Comment::class, null, function (Builder $builder) use ($postId) {
58+ return $builder->where('post_id', $postId);
59+ }),
60+ ],
61+ 'comments.*.content' => ['string']
62+ ];
63+ }
64+ ```
65+
66+ ** PostUpdateRequest**
1467
1568``` php
1669public function rules()
@@ -19,6 +72,7 @@ public function rules()
1972
2073 return [
2174 'id' => [new ExistEloquent(Post::class)],
75+ 'username' => [new UniqueEloquent(User::class, 'username')->ignore($postId)],
2276 'title' => ['string'],
2377 'content' => ['string'],
2478 'comments.*.id' => [
@@ -34,6 +88,8 @@ public function rules()
3488
3589## Contributing
3690
91+ I am open for suggestions and contributions. Just create an issue or a pull request.
92+
3793### Testing
3894
3995``` bash
@@ -48,6 +104,11 @@ composer fix
48104composer lint
49105```
50106
107+ ## Credits
108+
109+ The structure of the repository and the TestClass is inspired by the
110+ project [ laravel-validation-rules] ( https://github.com/spatie/laravel-validation-rules ) by [ spatie] ( https://github.com/spatie ) .
111+
51112## License
52113
53- The MIT License (MIT). Please see [ license file] ( license.md ) for more information.
114+ This package is licensed under the MIT License (MIT). Please see [ license file] ( license.md ) for more information.
0 commit comments