Download the PHP package toriomlab/eloquent-form-elements without Composer
On this page you can find all versions of the php package toriomlab/eloquent-form-elements. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download toriomlab/eloquent-form-elements
More information about toriomlab/eloquent-form-elements
Files in toriomlab/eloquent-form-elements
Package eloquent-form-elements
Short Description Simple eloquent model form elements generator package for Laravel 5.5
License MIT
Informations about the package eloquent-form-elements
Eloquent-Fields Generator Package For Laravel
This package actually contains a helper function and a trait. Its basic target is to reduce time that developers spend writing Creation & Update forms markup for Eloquent Models.
It's basic idea is to use the FormGenerator
trait in your model and write a tiny array with your fields' preferences. Then you can use the helper function to generate a creation or update form.
Installing.
Via composer
Then in your model use the trait toriomlab\EloquentFormElements\Traits\FormGenerator
like the following:
How to use it?
By defining a static array in your model called $fields
and its structure should be as following example.
This will generate a bootstrap form-group
div contains this field.
Then you can generate a creation form fields in your view files like the following example:
And you can create an update form fields by passing a second parameter contains the object id.
So this actually will generate an update form fields for the row which its id is 1.
Full Guide
$fields array writing rules.
- Must provide
label
. - Must provide
input
which can beinput
,select
ortextarea
.
If the input
index is input
you must provide a type
index to provide the input type like text
or number
or date
etc.
If the input
type is select
you can provide an options
index which will contain an array for the select dropdown options. Or you can provide a relation
index which will get options from a relation in the eloquent model.
Relation array
There's three types of Eloquent Relatioships supported till now in the package. You have to provide a type
index in the relation array to specify the relation type based on the following:
belongsTo
its type will beone
because we will only select one option in this case.hasMany
OrbelongsToMany
: their type will bemany
because we will select multiple values.
If the type
index in the relation array is one
you will need to provide
model
index which will be the relation model.column
which is the foreign key.selectFrom
which will be the column we need to display in the option from the relation model.valueFrom
which will be the column from the relational model that will be the value of the option of the select dropdown.
If the type
index in the relation array is many
you will need to provide
name
index which will be the name of the relation. For example if my model isUser
and it has manyRole
so the name of the relation will beroles
or whatever the name of the relation you created in theUser
model.
You will also need to provide a selectFrom
and valueFrom
as well.
If the input is a select
you can provide a valueFallback
which will be a static method returns all the options in the dropdown and then you need to provide a valueCallback
which will return the selected options collection.
Additional Preferences
There are multiple additional things that can be very usefull in all your fields specifications.
label_classes
which is the label class attribute value. You can override the default classes which arecontrol-label col-md-4
.input_div_classes
which is the class attribute of the div that contains the input code. By default it'scol-md-6
.input_classes
:which is the class attribute of the input itself. By default it'sform-control
.input_id
which is the id attribute of the input itself. By default it's the field name.inject_attributes
which allows you to add additional attributes to the input. It just takes the string and put it in the input tag.
Examples.
Generate normal text field:
Generate number field:
Generate HTML Image:
Generate HTML Line:
If the model belongs to a role and we wanna make a select field for the roles.
What if the model belongs to many roles?
A full example how should be the $fields array of a user model.
Example for valueCallbacks and valueFallbacks.
Example for manual belongsTo
relation with createValueCallbacks and updateValueFallbacks.
Then you can just call generate_fields('App\User')
to generate a creation form fields or generate_fields('App\User', 1)
to generate an update form fields for User whose id is 1.
Notice that you can pass a third parameter to generate_fields
parameter to except one or many fields from being generated in the form.
So if we called generate_fields('App\User', null, 'age')
a creation form will be created without age
field. You can also pass an array so generate_fields('App\User', null, ['age', 'roles'])
will create a creation form without roles and age fields.