laracraft-tech/laravel-schema-rules
Generate starter Laravel validation rules from your database schema. Create rules for entire tables or selected columns, optionally generate Form Request classes, and configure columns to always skip. Great for fast scaffolding before manual refinement.
Installation:
composer require laracraft-tech/laravel-schema-rules --dev
php artisan vendor:publish --tag="schema-rules-config"
First Use Case:
Generate validation rules for a table (e.g., users) directly in your terminal:
php artisan schema:generate-rules users
Copy-paste the output into your FormRequest or controller validation logic.
config/schema-rules.php (customize skip_columns, e.g., ['deleted_at', 'updated_at']).php artisan schema:generate-rules --help for all flags (e.g., --columns, --create-request).Rapid Prototyping:
Use --create-request to auto-generate a FormRequest class:
php artisan schema:generate-rules posts --create-request --file StorePostRequest
Output: app/Http/Requests/StorePostRequest.php with pre-filled rules.
Partial Validation:
Generate rules for specific columns (e.g., title, content):
php artisan schema:generate-rules posts --columns title,content
Integration with Existing Code:
rules() with generated rules (e.g., $this->rules = [...]).Validator::make($data, $rules).comments for a post).CI/CD Pipeline: Add a script to auto-generate rules during deployment:
php artisan schema:generate-rules users --create-request --force
Customize Rules:
Extend the generated rules in your FormRequest:
public function rules()
{
$rules = parent::rules(); // If using a base class
$rules['email'][] = 'unique:users'; // Add custom rules
return $rules;
}
Foreign Key Validation:
The package auto-generates exists:table,column rules for foreign keys (e.g., user_id → exists:users,id).
Nullable Fields:
Fields marked as nullable() in migrations get nullable in the rules (e.g., bio → ['nullable', 'string']).
Enum/Boolean Handling:
Enums generate in:value1,value2 rules, while booleans use boolean.
Floating-Point Precision:
float columns generate numeric rules (no precision constraints).digits:X,decimals:Y or use decimal columns instead.Driver-Specific Quirks:
max:255 for string columns (adjust if using TEXT).jsonb support for complex types (test with php artisan schema:generate-rules --columns json_column).Config Overrides:
skip_columns in config may not apply if using --columns.php artisan schema:generate-rules users --columns name,email --skip-columns deleted_at
FormRequest Naming Conflicts:
--create-request may overwrite existing files.--file to specify a custom path or --force to overwrite.Verify Schema:
Run php artisan schema:generate-rules users --columns all to debug column mappings.
Compare output with your migration file.
Check Database Connection:
Ensure your .env DB settings match the schema (e.g., DB_CONNECTION=mysql).
Log Generated Rules: Redirect output to a file for review:
php artisan schema:generate-rules users > rules.txt
Custom Rule Mappings: Override default rules by extending the package:
// app/Providers/AppServiceProvider.php
use LaracraftTech\SchemaRules\RuleGenerator;
public function boot()
{
RuleGenerator::macro('customRule', function ($column) {
return ['custom' => 'validation'];
});
}
Add New Drivers:
Extend the DatabaseSchema class to support unsupported drivers (e.g., SQL Server).
Post-Generation Hooks: Use Laravel events to modify rules after generation:
// config/schema-rules.php
'post_generate_hook' => App\Services\RuleModifier::class,
Combine with Laravel IDE Helper:
Generate rules during development, then use barryvdh/laravel-ide-helper to auto-complete validation rules in your IDE.
Version Control:
Commit generated FormRequest files to enforce consistency across environments.
Testing: Use the generated rules in PHPUnit tests:
$validator = Validator::make($data, $this->rules());
$validator->validate();
How can I help you explore Laravel packages today?