7
7
[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/laracraft-tech/laravel-schema-rules.svg?style=flat-square )] ( https://packagist.org/packages/laracraft-tech/laravel-schema-rules )
8
8
9
9
Automatically generate basic Laravel validation rules based on your database table schema!
10
- Use these as a starting point to fine-tune and optimize your validation rules as needed.
10
+ Use these as a starting point to fine-tune and optimize your validation rules as needed.
11
11
12
12
Here you can use the web version, if you like: [ https://validationforlaravel.com ] ( https://validationforlaravel.com )
13
13
@@ -27,15 +27,27 @@ php artisan vendor:publish --tag="schema-rules-config"
27
27
28
28
## ToC
29
29
30
- - [ ` Generate rules for a whole table ` ] ( #generate-rules-for-a-whole-table )
31
- - [ ` Generate rules for specific columns ` ] ( #generate-rules-for-specific-columns )
32
- - [ ` Generate Form Request Class ` ] ( #generate-form-request-class )
30
+ - [ Laravel Schema Rules] ( #laravel-schema-rules )
31
+ - [ Installation] ( #installation )
32
+ - [ ToC] ( #toc )
33
+ - [ Usage] ( #usage )
34
+ - [ Generate rules for a whole table] ( #generate-rules-for-a-whole-table )
35
+ - [ Generate rules for specific columns] ( #generate-rules-for-specific-columns )
36
+ - [ Generate Form Request Class] ( #generate-form-request-class )
37
+ - [ Always skip columns] ( #always-skip-columns )
38
+ - [ Supported Drivers] ( #supported-drivers )
39
+ - [ Testing] ( #testing )
40
+ - [ Changelog] ( #changelog )
41
+ - [ Contributing] ( #contributing )
42
+ - [ Security Vulnerabilities] ( #security-vulnerabilities )
43
+ - [ Credits] ( #credits )
44
+ - [ License] ( #license )
33
45
34
46
## Usage
35
47
36
48
Let's say you've migrated this fictional table:
37
49
38
- ```` php
50
+ ``` php
39
51
Schema::create('persons', function (Blueprint $table) {
40
52
$table->id();
41
53
$table->string('first_name', 100);
@@ -52,7 +64,7 @@ Schema::create('persons', function (Blueprint $table) {
52
64
$table->unsignedInteger('net_income');
53
65
$table->boolean('send_newsletter')->nullable();
54
66
});
55
- ````
67
+ ```
56
68
57
69
### Generate rules for a whole table
58
70
@@ -61,6 +73,7 @@ Now if you run:
61
73
` php artisan schema:generate-rules persons `
62
74
63
75
You'll get:
76
+
64
77
```
65
78
Schema-based validation rules for table "persons" have been generated!
66
79
Copy & paste these to your controller validation or form request or where ever your validation takes place:
@@ -82,7 +95,7 @@ Copy & paste these to your controller validation or form request or where ever y
82
95
```
83
96
84
97
As you may have noticed the float-column ` body_size ` , just gets generated to ` ['required', 'numeric'] ` .
85
- Proper rules for ` float ` , ` decimal ` and ` double ` , are not yet implemented!
98
+ Proper rules for ` float ` , ` decimal ` and ` double ` , are not yet implemented!
86
99
87
100
### Generate rules for specific columns
88
101
@@ -91,28 +104,29 @@ You can also explicitly specify the columns:
91
104
` php artisan schema:generate-rules persons --columns first_name,last_name,email `
92
105
93
106
Which gives you:
94
- ````
107
+
108
+ ```
95
109
Schema-based validation rules for table "persons" have been generated!
96
110
Copy & paste these to your controller validation or form request or where ever your validation takes place:
97
111
[
98
112
'first_name' => ['required', 'string', 'min:1', 'max:100'],
99
113
'last_name' => ['required', 'string', 'min:1', 'max:100'],
100
114
'email' => ['required', 'string', 'min:1', 'max:255']
101
115
]
102
- ````
116
+ ```
103
117
104
118
### Generate Form Request Class
105
119
106
120
Optionally, you can add a ` --create-request ` or ` -c ` flag,
107
121
which will create a form request class with the generated rules for you!
108
122
109
- ```` bash
123
+ ``` bash
110
124
# creates app/Http/Requests/StorePersonRequest.php (store request is the default)
111
- php artisan schema:generate-rules persons --create-request
125
+ php artisan schema:generate-rules persons --create-request
112
126
113
127
# creates/overwrites app/Http/Requests/StorePersonRequest.php
114
128
php artisan schema:generate-rules persons --create-request --force
115
-
129
+
116
130
# creates app/Http/Requests/UpdatePersonRequest.php
117
131
php artisan schema:generate-rules persons --create-request --file UpdatePersonRequest
118
132
@@ -121,7 +135,7 @@ php artisan schema:generate-rules persons --create-request --file Api\\V1\\Store
121
135
122
136
# creates/overwrites app/Http/Requests/Api/V1/StorePersonRequest.php (using shortcuts)
123
137
php artisan schema:generate-rules persons -cf --file Api\\ V1\\ StorePersonRequest
124
- ````
138
+ ```
125
139
126
140
### Always skip columns
127
141
@@ -131,7 +145,6 @@ To always skip columns add it in the config file, under `skip_columns` parameter
131
145
'skip_columns' => ['whatever', 'some_other_column'],
132
146
```
133
147
134
-
135
148
## Supported Drivers
136
149
137
150
Currently, the supported database drivers are ` MySQL ` , ` PostgreSQL ` , and ` SQLite ` .
@@ -141,6 +154,8 @@ the validation rules generated by this package may vary depending on the databas
141
154
142
155
## Testing
143
156
157
+ Before running tests, you need to set up a local MySQL database named ` laravel_schema_rules ` and update its _ username_ and _ password_ in the ` phpunit.xml.dist ` file.
158
+
144
159
``` bash
145
160
composer test
146
161
```
@@ -159,8 +174,8 @@ Please review [our security policy](../../security/policy) on how to report secu
159
174
160
175
## Credits
161
176
162
- - [ Zacharias Creutznacher] ( https://github.com/laracraft-tech )
163
- - [ All Contributors] ( ../../contributors )
177
+ - [ Zacharias Creutznacher] ( https://github.com/laracraft-tech )
178
+ - [ All Contributors] ( ../../contributors )
164
179
165
180
## License
166
181
0 commit comments