1
1
# Laravel model validation rules
2
2
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
+
3
16
## Installation
4
17
18
+ You can install the package via composer with following command:
19
+
5
20
``` bash
6
21
composer require korridor/laravel-model-validation-rules
7
22
```
8
23
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
+
9
29
``` bash
10
30
php artisan vendor:publish --provider=" Korridor\LaravelModelValidationRules\ModelValidationServiceProvider"
11
31
```
12
32
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**
14
67
15
68
``` php
16
69
public function rules()
@@ -19,6 +72,7 @@ public function rules()
19
72
20
73
return [
21
74
'id' => [new ExistEloquent(Post::class)],
75
+ 'username' => [new UniqueEloquent(User::class, 'username')->ignore($postId)],
22
76
'title' => ['string'],
23
77
'content' => ['string'],
24
78
'comments.*.id' => [
@@ -34,6 +88,8 @@ public function rules()
34
88
35
89
## Contributing
36
90
91
+ I am open for suggestions and contributions. Just create an issue or a pull request.
92
+
37
93
### Testing
38
94
39
95
``` bash
@@ -48,6 +104,11 @@ composer fix
48
104
composer lint
49
105
```
50
106
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
+
51
112
## License
52
113
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