Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lumen: Generate Model validation rules

Tags:

laravel

lumen

Artisan generator seems over-sophisticated, It generates a class extended from Model class!!!

Is there any way to generate model validation rules in a lumen model automatically (based on column definition of a mysql table)?

What about column names?

like image 314
hpaknia Avatar asked Oct 21 '25 11:10

hpaknia


2 Answers

I am the author of lumen-generators, A collection of generators for Lumen and Laravel 5.

This package contains a Model Generator which supports generating validation rules.

Installation

Add the generators package to your composer.json by running the command:

composer require wn/lumen-generators

Then add the service provider in the file app/Providers/AppServiceProvider.php like the following:

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Wn\Generators\CommandsServiceProvider');
    }
}

Don't forget to include the application service provider on your bootstrap/app.php and to enable Eloquent and Facades if you are using Lumen

If you run the command php artisan list you will see the list of added commands:

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.

Generating a model with validation rules

Runing the following command:

php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address"

Will generate a model containing the following rules:

public static $rules = [
    "name" => "required",
    "age" => "integer|min:13",
    "email" => "email|unique:users,email_address",
];

Please refer to the Full README for more details.

Hope this helps :)

like image 68
webNeat Avatar answered Oct 23 '25 04:10

webNeat


There is no such command built into laravel or lumen.

I found a package (on a site called google) that provides such a command: https://github.com/jijoel/validation-rule-generator

It's locked to illuminate/support 4.0.x, so won't work with current versions of laravel. If you have lots of models it might be worth to fork, bump the version in composer.json and see if it works.

like image 27
jsphpl Avatar answered Oct 23 '25 04:10

jsphpl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!