laracraft-tech/laravel-schema-rules
Generate basic Laravel validation rules from your database schema. Create rules for entire tables or selected columns, scaffold Form Request classes, and configure columns to always skip. Great as a starting point to refine and optimize validation.
Installation:
composer require laracraft-tech/laravel-schema-rules --dev
php artisan vendor:publish --tag="schema-rules-config"
Verify the config file is published to config/schema-rules.php.
First Use Case:
Generate validation rules for an existing table (e.g., users):
php artisan schema:generate-rules users
Output will be a JSON array of validation rules for each column, ready to paste into your controller or Form Request.
php artisan schema:generate-rules --help to explore all flags.config/schema-rules.php for customizing skipped columns or default behavior.users).php artisan migrate.php artisan schema:generate-rules users
Copy the output into your StoreUserRequest or controller validation.Generate a Form Request class directly:
php artisan schema:generate-rules users --create-request --file StoreUserRequest
This creates app/Http/Requests/StoreUserRequest.php with pre-filled rules.
Customize the Request: Extend the generated class to add custom logic:
public function rules()
{
$rules = parent::rules(); // Inherit schema rules
$rules['email'][] = 'unique:users'; // Add custom rule
return $rules;
}
Generate rules for specific columns (e.g., name and email):
php artisan schema:generate-rules users --columns name,email
Create a request for an API namespace:
php artisan schema:generate-rules users --create-request --file Api\\V1\\StoreUserRequest
Use in Controllers:
public function store(Request $request)
{
$validated = $request->validate(
$this->generateSchemaRules('users') // Assume this method calls the artisan command
);
}
Combine with Custom Rules: Override generated rules in your Form Request:
public function rules()
{
$schemaRules = $this->getSchemaRules('users');
return array_merge($schemaRules, [
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
Dynamic Rule Generation: Create a helper method in a trait or service:
public function getSchemaRules(string $table): array
{
$command = new GenerateRulesCommand();
return $command->getRules($table);
}
Testing: Use the generated rules in PHPUnit tests:
public function test_validation()
{
$rules = $this->getSchemaRules('users');
$validator = Validator::make($data, $rules);
$validator->validate();
}
Floating-Point Precision:
The package generates ['numeric'] for float, decimal, and double columns. For precise validation, manually add rules like ['numeric', 'min:0.00', 'max:100.00'].
Driver-Specific Quirks:
max:255 for string columns (adjust if needed).jsonb columns generate ['json'] rules.string columns.Foreign Key Validation:
The package generates exists:table,column rules, but ensure the referenced table exists and the column matches.
Nullable Fields:
The package adds ['nullable'] for nullable columns, but be cautious with required rules in Form Requests. Use sometimes or conditional validation if needed:
public function rules()
{
$rules = $this->getSchemaRules('users');
$rules['bio'] = array_filter($rules['bio'], fn($rule) => $rule !== 'nullable');
return $rules;
}
Config Overrides:
If you skip columns in config/schema-rules.php, ensure they are excluded from migrations or handled separately (e.g., deleted_at for soft deletes).
Check Column Types: If rules seem incorrect, verify the column type in your migration or database schema:
php artisan schema:show users
Artisan Verbosity:
Use -v or -vv flags to debug:
php artisan schema:generate-rules users -vv
Custom Rule Logic:
Extend the package by publishing and modifying its service provider or command. Override the getRules method in GenerateRulesCommand.
Skip Timestamps:
The config defaults to skip created_at, updated_at, and deleted_at. Add more to skip_columns if needed:
'skip_columns' => [
'created_at',
'updated_at',
'deleted_at',
'user_id', // Custom skip
],
Web Interface: Use validationforlaravel.com to preview rules before generating them.
CI/CD Integration: Add the command to your deployment pipeline to ensure validation rules stay in sync with the schema:
# .github/workflows/deploy.yml
- run: php artisan schema:generate-rules users --create-request
Partial Overrides: Generate rules once, then manually tweak them in your Form Request:
public function rules()
{
return array_merge(
$this->getSchemaRules('users'),
['email' => ['unique:users']]
);
}
Laravel 11+ Compatibility:
The package supports Laravel 11–13. For older versions, pin to v1.3.x or earlier.
Performance: Avoid generating rules on every request. Cache the output or regenerate only during migrations:
$rules = cache()->remember("schema-rules-{$table}", now()->addHours(1), fn() =>
$this->getSchemaRules($table)
);
How can I help you explore Laravel packages today?