appoly / smart-schema
Form helper for Laravel
Installs: 2 773
Dependents: 0
Suggesters: 0
Security: 0
Stars: 18
Watchers: 7
Forks: 1
Open Issues: 2
- dev-master
- 0.7.12
- 0.7.11
- 0.7.10
- 0.7.9
- 0.7.8
- 0.7.7
- 0.7.6
- 0.7.5
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.1
- 0.5.0
- 0.4.13
- 0.4.12
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.0
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.2
- 0.0.1
- dev-dependabot/add-v2-config-file
- dev-analysis-XWRgRP
This package is auto-updated.
Last update: 2024-10-29 04:53:31 UTC
README
Tired of repeating yourself? This package centralises everything to do with fields.
Navigation
Quick Usage
Add to composer.json
:
"appoly/smart-schema": "^0.6.3",
Introduction
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();
});
Options for fields
Field Types
Standard field types area available.
$table->text("name")
$table->integer("user_id")
$table->float("latitude")
etc...
Virtual Fields
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(...
Validation Rules
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
// ...
}
Model attributes
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;
}
Forms
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'
]
]
]) !!}
Code Generation
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