77[ ![ 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 )
88
99Automatically 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.
1111
1212Here you can use the web version, if you like: [ https://validationforlaravel.com ] ( https://validationforlaravel.com )
1313
@@ -27,15 +27,27 @@ php artisan vendor:publish --tag="schema-rules-config"
2727
2828## ToC
2929
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 )
3345
3446## Usage
3547
3648Let's say you've migrated this fictional table:
3749
38- ```` php
50+ ``` php
3951Schema::create('persons', function (Blueprint $table) {
4052 $table->id();
4153 $table->string('first_name', 100);
@@ -52,7 +64,7 @@ Schema::create('persons', function (Blueprint $table) {
5264 $table->unsignedInteger('net_income');
5365 $table->boolean('send_newsletter')->nullable();
5466});
55- ````
67+ ```
5668
5769### Generate rules for a whole table
5870
@@ -61,6 +73,7 @@ Now if you run:
6173` php artisan schema:generate-rules persons `
6274
6375You'll get:
76+
6477```
6578Schema-based validation rules for table "persons" have been generated!
6679Copy & 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
8295```
8396
8497As 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!
8699
87100### Generate rules for specific columns
88101
@@ -91,28 +104,29 @@ You can also explicitly specify the columns:
91104` php artisan schema:generate-rules persons --columns first_name,last_name,email `
92105
93106Which gives you:
94- ````
107+
108+ ```
95109Schema-based validation rules for table "persons" have been generated!
96110Copy & paste these to your controller validation or form request or where ever your validation takes place:
97111[
98112 'first_name' => ['required', 'string', 'min:1', 'max:100'],
99113 'last_name' => ['required', 'string', 'min:1', 'max:100'],
100114 'email' => ['required', 'string', 'min:1', 'max:255']
101115]
102- ````
116+ ```
103117
104118### Generate Form Request Class
105119
106120Optionally, you can add a ` --create-request ` or ` -c ` flag,
107121which will create a form request class with the generated rules for you!
108122
109- ```` bash
123+ ``` bash
110124# 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
112126
113127# creates/overwrites app/Http/Requests/StorePersonRequest.php
114128php artisan schema:generate-rules persons --create-request --force
115-
129+
116130# creates app/Http/Requests/UpdatePersonRequest.php
117131php artisan schema:generate-rules persons --create-request --file UpdatePersonRequest
118132
@@ -121,7 +135,7 @@ php artisan schema:generate-rules persons --create-request --file Api\\V1\\Store
121135
122136# creates/overwrites app/Http/Requests/Api/V1/StorePersonRequest.php (using shortcuts)
123137php artisan schema:generate-rules persons -cf --file Api\\ V1\\ StorePersonRequest
124- ````
138+ ```
125139
126140### Always skip columns
127141
@@ -131,7 +145,6 @@ To always skip columns add it in the config file, under `skip_columns` parameter
131145'skip_columns' => ['whatever', 'some_other_column'],
132146```
133147
134-
135148## Supported Drivers
136149
137150Currently, 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
141154
142155## Testing
143156
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+
144159``` bash
145160composer test
146161```
@@ -159,8 +174,8 @@ Please review [our security policy](../../security/policy) on how to report secu
159174
160175## Credits
161176
162- - [ Zacharias Creutznacher] ( https://github.com/laracraft-tech )
163- - [ All Contributors] ( ../../contributors )
177+ - [ Zacharias Creutznacher] ( https://github.com/laracraft-tech )
178+ - [ All Contributors] ( ../../contributors )
164179
165180## License
166181
0 commit comments