Artisan 生成器似乎过于复杂,它生成一个从 Model 类扩展而来的类!!!
有没有办法在lumen模型中自动生成模型验证规则(基于mysql表的列定义)?
列名怎么样?
我是 lumen-generators 的作者,这是 Lumen 和 Laravel 5 的生成器集合。
此软件包包含一个模型生成器,支持生成验证规则。
通过运行以下命令将生成器包添加到您的composer.json:
composer require wn/lumen-generators
然后在文件中添加服务提供商
app/Providers/AppServiceProvider.php
,如下所示:
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register('Wn\Generators\CommandsServiceProvider');
}
}
不要忘记在您的
bootstrap/app.php
中包含应用程序服务提供商,如果您使用的是 Lumen,请启用 Eloquent 和 Facades
如果运行命令
php artisan list
,您将看到添加的命令列表:
wn:controller Generates RESTful controller using the RESTActions trait
wn:controller:rest-actions Generates REST actions trait to use into controllers
wn:migration Generates a migration to create a table with schema
wn:model Generates a model class for a RESTfull resource
wn:pivot-table Generates creation migration for a pivot table
wn:resource Generates a model, migration, controller and routes for RESTful resource
wn:resources Generates multiple resources from a file
wn:route Generates RESTful routes.
运行以下命令:
php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address"
将生成包含以下规则的模型:
public static $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
请参阅完整自述文件了解更多详细信息。
希望这有帮助:)
laravel 或 lumen 中没有内置这样的命令。
我发现一个包(在一个名为google的网站上)提供了这样的命令:https://github.com/jijoel/validation-rule-generator
它已锁定照亮/支持 4.0.x,因此不适用于当前版本的 laravel。如果你有很多模型,可能值得分叉,在composer.json中修改版本并看看它是否有效。
查看 Laravel 架构规则:
https://github.com/laracraft-tech/laravel-schema-rules
安装
composer require laracraft-tech/laravel-schema-rules --dev
假设您已经迁移了这个虚构的表:
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();
});
现在如果你跑步:
php artisan schema:generate-rules persons
你会得到:
Schema-based validation rules for table "persons" have been generated!
Copy & paste these to your controller validation or form request or where ever your validation takes place:
[
'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']
]