Laravel Schema Rules 是一个根据数据库表架构自动生成基本 Laravel 验证规则的包。您可以使用此包作为起点来快速创建样板规则并根据需要进行优化。
给定您的表的以下架构(来自自述文件):
Schema::create('persons', function (Blueprint $table) {
$table->id();
$table->string('first_name', 100);
$table->string('last_name', 100);
$table->string('email');
$table->foreignId('address_id')->constrained();
$table->text('bio')->nullable();
$table->enum('gender', ['m', 'f', 'd']);
$table->date('birth');
$table->year('graduated');
$table->float('body_size');
$table->unsignedTinyInteger('children_count')->nullable();
$table->integer('account_balance');
$table->unsignedInteger('net_income');
$table->boolean('send_newsletter')->nullable();
});
您可以运行 schema:generate-rules
Artisan 命令来生成验证规则:
$ php artisan schema:generate-rules persons
[
'first_name' => ['required', 'string', 'min:1', 'max:100'],
'last_name' => ['required', 'string', 'min:1', 'max:100'],
'email' => ['required', 'string', 'min:1', 'max:255'],
'address_id' => ['required', 'exists:addresses,id'],
'bio' => ['nullable', 'string', 'min:1'],
'gender' => ['required', 'string', 'in:m,f,d'],
'birth' => ['required', 'date'],
'graduated' => ['required', 'integer', 'min:1901', 'max:2155'],
'body_size' => ['required', 'numeric'],
'children_count' => ['nullable', 'integer', 'min:0', 'max:255'],
'account_balance' => ['required', 'integer', 'min:-2147483648', 'max:2147483647'],
'net_income' => ['required', 'integer', 'min:0', 'max:4294967295'],
'send_newsletter' => ['nullable', 'boolean']
]
您还可以定位特定列、跳过列,甚至生成表单请求类。
该包还有一个在线版本,您可以使用它来试验该包或使用它手动获取模式并将其转换为验证规则数组:
访问validationforlaravel.com 来尝试一下!
您可以了解有关此软件包的更多信息、获取完整的安装说明并在 GitHub 上查看源代码。
https://github.com/laracraft-tech/laravel-schema-rules