Form helper for Laravel
Tired of repeating yourself? This package centralises everything to do with fields., (*1)
Navigation - Introduction - Field Types - Virtual Fields - Validation Rules - Model Attributes - Forms - Code Generation, (*2)
Add to composer.json
:, (*3)
"appoly/smart-schema": "^0.6.3",
Instead of having to create a migration, a request, form views and set up fillable fields, we can instead create a smart migration which handles it all., (*4)
Example migration:, (*5)
SmartSchema::create('sites', function ($table) { $table->increments("id"); $table->integer("name"); $table->text("name") ->nullable() ->required() ->fillable() ->max(5) ->forms([ 'type' => 'text', 'label' => 'Site name' ]); $table->timestamps(); });
Standard field types area available., (*6)
$table->text("name") $table->integer("user_id") $table->float("latitude")
etc..., (*7)
In some cases, we may want fields in a form that don't correspond directy to our database tables., (*8)
We can then use:, (*9)
$table->virtual("slot")->forms(...
These can be chained to a field creation in your migration., (*10)
Example:, (*11)
$table->text("email")->required()->email();
Available rules:, (*12)
->unique() ->required() ->email() ->max( val ) ->min( val )
Custom validation rules can be added with:, (*13)
->addRule( rule )
When storing the object in your controller, the validation helper should be called with the object type:, (*14)
public function store(Request $request) { SchemaHelper::validate($request, 'sites'); // Process the request // ... }
fillable()
, (*15)
Casts:, (*16)
->array() ->datetime()
Model must have the SmartField
trait to use fillable()
or any of the attribute casts., (*17)
class User extends Model { use SmartField; }
The form helper will generate (currently Bootstrap 4) forms based on the field data., (*18)
In migration, use ->forms([...
to show a field in the auto-generated forms:, (*19)
->forms([ 'type' => 'text', 'label' => 'Site name' ])
To render a basic form:, (*20)
{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store')) !!}
Multiple choice form fields such as selects and radio buttons will need a set of options., (*21)
In the case of the following field:, (*22)
$table->id("role") ->forms([ 'type' => 'select', // or 'type' => 'radio' 'label' => 'Role' ]);
Options can be passed like so:, (*23)
{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store'), [ 'select_options' => [ 'role_id' => ['User', 'Admin'] ] ]) !!}
Or with keys, (*24)
{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store'), [ 'select_options' => [ 'role_id' => [ 10001 => 'User', 20001 => 'Admin' ] ] ]) !!}
This package includes a console command which will set up a boilerplate controller and view code., (*25)
php artisan crud:generate {resource_name_singular}
, (*26)
For example:, (*27)
php artisan crud:generate client
, (*28)