Lumen:生成模型验证规则

时间:2016-08-30 23:54:38

标签: laravel lumen

Artisan生成器看起来过于复杂,它生成了一个从Model类扩展的类!!!

  

有没有办法在流明模型中自动生成模型验证规则(基于mysql表的列定义)?

     

列名怎么样?

2 个答案:

答案 0 :(得分:4)

我是lumen-generators的作者,Lumen和Laravel 5的发电机集合。

此软件包包含Model Generator,支持生成验证规则。

安装

通过运行以下命令将生成器包添加到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上添加应用服务提供商,如果使用流明,则启用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

希望这会有所帮助:)

答案 1 :(得分:1)

laravel或lumen中没有这样的命令。

我找到了一个包(在名为google的网站上)提供了这样一个命令:https://github.com/jijoel/validation-rule-generator

它被锁定以照亮/支持4.0.x,因此无法使用当前版本的laravel。如果你有很多模型可能值得分叉,请在composer.json中查看版本,看它是否有效。

相关问题