手工生成器似乎过于复杂,它生成一个从模型类扩展的类!
是否有任何方法自动生成流明模型中的模型验证规则(基于mysql表的列定义)? 那列名呢?
发布于 2017-01-25 15:12:59
我是“流明发生器,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,则要启用雄辩的外观
如果运行命令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",
];有关更多细节,请参考完全自述。
希望这会有所帮助:)
发布于 2016-08-31 04:51:20
没有这样的命令内置在拉拉维尔或内腔。
我找到了一个提供这样一个命令的包(在一个名为google的站点上):https://github.com/jijoel/validation-rule-generator
它被锁定为照明/支持4.0.x,因此不能使用当前版本的laravel。如果您有很多模型,那么分叉可能是值得的,在composer.json中使用这个版本,看看它是否有效。
https://stackoverflow.com/questions/39238758
复制相似问题