Tired of repeating yourself? This package centralises everything to do with fields.
Navigation
Add to composer.json
:
"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.
Example migration:
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.
$table->text("name")
$table->integer("user_id")
$table->float("latitude")
etc...
In some cases, we may want fields in a form that don't correspond directy to our database tables.
We can then use:
$table->virtual("slot")->forms(...
These can be chained to a field creation in your migration.
Example:
$table->text("email")->required()->email();
Available rules:
->unique()
->required()
->email()
->max( val )
->min( val )
Custom validation rules can be added with:
->addRule( rule )
When storing the object in your controller, the validation helper should be called with the object type:
public function store(Request $request) {
SchemaHelper::validate($request, 'sites');
// Process the request
// ...
}
fillable()
Casts:
->array()
->datetime()
Model must have the SmartField
trait to use fillable()
or any of the attribute casts.
class User extends Model
{
use SmartField;
}
The form helper will generate (currently Bootstrap 4) forms based on the field data.
In migration, use ->forms([...
to show a field in the auto-generated forms:
->forms([
'type' => 'text',
'label' => 'Site name'
])
To render a basic form:
{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store')) !!}
Multiple choice form fields such as selects and radio buttons will need a set of options.
In the case of the following field:
$table->id("role")
->forms([
'type' => 'select', // or 'type' => 'radio'
'label' => 'Role'
]);
Options can be passed like so:
{!! \Appoly\SmartSchema\SchemaHelper::form('sites', route('sites.store'), [
'select_options' => [
'role_id' => ['User', 'Admin']
]
]) !!}
Or with keys
{!! \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.
php artisan crud:generate {resource_name_singular}
For example:
php artisan crud:generate client