laravel 内腔:生成模型验证规则

tyg4sfes  于 2023-10-22  发布在  其他
关注(0)|答案(3)|浏览(194)

Artisan生成器似乎过于复杂,它生成一个从Model类扩展的类!!!
有没有什么方法可以在一个流明模型中自动生成模型验证规则(基于mysql表的列定义)?
列名呢?

abithluo

abithluo1#

我是lumen-generators的作者,Lumen和Laravel 5的生成器集合。
此软件包包含一个模型生成器,支持生成验证规则。

安装

通过运行以下命令将generators包添加到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');
    }
}

如果使用Lumen,请不要忘记在bootstrap/app.php上包含应用程序服务提供商,并启用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",
];

请参阅Full README了解更多详情。
希望这对你有帮助:)

xesrikrc

xesrikrc2#

laravel或lumen中没有内置这样的命令。
我发现一个软件包(在一个叫google的网站上)提供了这样一个命令:https://github.com/jijoel/validation-rule-generator
它被锁定为illuminate/支持4.0.x,因此不能与当前版本的laravel一起工作。如果你有很多模型,可能值得分叉,在composer.json中碰撞版本,看看它是否有效。

prdp8dxp

prdp8dxp3#

CheckoutLaravel Schema Rules
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']
]

相关问题