rashidul / raindrops
Laravel package to help building enterprize applications
Installs: 784
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 3
Open Issues: 0
Language:JavaScript
pkg:composer/rashidul/raindrops
Requires
Suggests
- nwidart/laravel-modules: Module Management In Laravel
This package is auto-updated.
Last update: 2025-10-06 22:50:55 UTC
README
(Work in Progress)
Installation
-
Inside your project root, run
composer require rashidul/raindrops=dev-master -
Add this to your
app.phpconfig file'sprovidersarrayRashidul\RainDrops\RainDropsServiceProvider::class, -
Optionally, you can add this to
aliasesarray for simplicity'FormBuilder' => Rashidul\RainDrops\Facades\FormBuilder::class, -
Finally, publish the config files.
php artisan vendor:publish --provider=Rashidul\RainDrops\RainDropsServiceProvider
Generating Form
To generate a form for a model, you first need to add a $fields public property to your model and populate it with required informations.
Then you can generate the form markup by simply calling the build method on FormBuilder providing an instance of your model
Example:
In your controller:
$client = new Client(); /* or, $client = Client::find(<id>); if you are editing ane existing record */ $form = FormBuilder::build( $client ) ->form([ 'action' => 'clients', 'method' => 'POST' ]) ->render(); return view('create', compact('form'));
don't forget to use the FormBuilder class at the top of your model
Then in your view, you can do:
{!! $form !!}
That's it!
Form Configuration
There are some helpful methods to further customize the form.
-
add( $field_name, $options = [] )You can add fields to the existing fields which are defined in your model's$fieldsproperty.$form = FormBuilder::build( $client ) ->form([ 'action' => 'clients', 'method' => 'POST' ]) ->add('field_name', [ 'label' => 'Field Label', 'type' => 'date' ]) ->render();
first argument is the field's
nameand second one is an array containing various options for that field.label&typefields are mandatory -
modify( $field_name, $options = [] )You can modify any defined field in the model$form = FormBuilder::build( $client ) ->form([ 'action' => 'clients', 'method' => 'POST' ]) ->modify('field_name', [ 'label' => 'New changed label', 'required' => true ]) ->render();
You need to provide only the options you want to modify/override.
-
section( $header, $fields = [] )Grouping some fields together with a header.$form = FormBuilder::build( $client ) ->form([ 'action' => 'clients', 'method' => 'POST' ]) ->section('Awesome Section One', [ 'field_one', 'field_two']) ->section('Awesome Section Two', [ 'field_three', 'field_four']) ->render(); ```
-
template( $name )Name of the template to be used to generate a single field. This is defined in theformconfig file insideraindropsdirectory. You can use multiple templates to create two-column/three column layout. -
remove( $field_name )To remove any field. you can also pass an array of field name to be excluded in the final generated form. -
csrf( boolean )By default, a csrf field will be generated. you can exclude it by passingfalse -
only( $fields = [] )Select only some of the fields to be generated. -
hidden( $name, $value )Add hidden field to the form. -
submitTo configure the submit option.$form = FormBuilder::build( $client ) ->form([ 'action' => 'clients', 'method' => 'POST' ]) ->submit([ 'text' => 'Save Me', 'class' => 'btn btn-success', 'icon' => 'fa fa-save' ]) ->render();
Index Page
For index page, we'll use datatable jQuery plugin. First include the necessary js/css files for the datatable plugin.
Generating table markup/scripts
$table = DataTable::of(new Client) ->setUrl(url('datatables.data')) ->setId('data-table') ->render(); return view('index', compact('table'));
In the view file: {!! $table !!}
Handling backend
public function anyData(Datatables $datatables) { $query = Client::select(); return $datatables->eloquent($query) ->setTransformer(new DataTableTransformer()) ->make(true); }
CRUD Generator
Our model will have a array property called $fields which will hold all the information we need
to generate forms/list pages/filter options etc. later on.
$fields array will contain the model's database fields as key and another configuration array as value.
public $fields = [ 'field_name' => [ 'label' => 'Field Label', 'type' => 'input type' /* other configurations goes here */ ] ]
Options
-
label(required)label which will be used to create label tag of the form element. also for creating table header in list page.
-
type(required)Form input type for a specific field. Currently implemented types:
- basic html input types :
text,email,password,file,textarea - rich text editor:
editor - select dropdown:
select - populating dropdown from another database table(foreign key):
select_db - datepicker:
date - timepicker:
time
- basic html input types :
These two fields are required to generate a generic form input element. Below options can be used for further customization of the field.
-
formSet this to
falseif you don't want to create a form input field for this particular field. -
indexAdd a
indexkey and set it totrueif you want this field to be shown on the list table. -
requiredThis will be
trueif this field is required.The html form input element will have arequiredattribute for basic client side validations.TODO:later we need to bring in fromvalidationsoption so that we don't need to specify required seperately. -
validationsValidation rules for this field.The format is exactly like laravel's default validation rules format with only one minor difference. When using
uniquerule, add a{id}add the end of the rule to ignore this model's ID while updating e.g.unique:<table_name>,<column_name>{id} -
classes(TODO)If you want to use any custom classes for any particular field, you can add a
classesoption. Pass a comma seperated string of class names. -
attributes(TODO)Any extra attributes to generated for the input field.This can be particularly helpful to customize the input with default html attributes, e.g. specifying
min&maxattribute for anumberfield. just pass an array with the attributes name/value as key/value pair. -
showThis option is to determine how the field's data should be displayed. This is used both for the list page table and for the details page of resource.
currently supported options
exactshow the value exactly as databae without any modifications.stringwill pass the value through ucfirst. helpful to be used with name type fields.details_linkas a link to the details page of the resource.urljust wrap the value inside an anchor tag.telanchor tag for phone numbers, should be used to make phone numbers clickable on mobile devices.mailtomailto: links, for email address.enumif the field type isselectthis option will get the actual text from theoptionsarray.imgto wrap the value around an image tag. to be used with image url fields.docif the field is a url for any downloadable resource, this will provide option to download it.timedatetimerelationwhen the field is actually a foreign key for another table, you can use the relation to show any column from that related model. add an array as the value of therelationoption. first element is the name of the relation and the second option is the column name from that related model.
-
filterWhat type of input element is to be generated for filtering this field.
Currently suported
enumdropdown with options populated fromoptionselementdatefor any type of date fields. it will generate afield_name-fromandfield_name-todate picker field.TODO:need a to implement a daterange picker plugin for this.booleanthis will generate a checkbox. to be used with any flag type fields.
The following options are required based on the value of type
-
optionsThis option is required when you set
selectorselect_dbas the input type. Forselect, you need to specify dropdown's value's as an array.e.g.
'type' => 'select', 'options' => [ 'option_1' => 'Option One' ]
this will generate an option tag for the select tag:
<option value="option_1">Option One</option>
for
select_db,optionswill be a simple array. first element is the name of the db field, which will be used as the option's value.(generally theidcolumn of the table) and second element is the column with option's value. if you pass more than two column names, all those will be concatenated with a space and displayed as option text. -
tableThe name of the table to be used with
select_dboption.
Example
/** * Fields */ public $fields = [ 'public_id' => [ 'label' => 'Device ID', 'form' => false, 'index' => true, 'show' => 'exact' ], 'serial_number' => [ 'label' => 'Serial Number', 'type' => 'text', 'validations' => 'unique:devices,serial_number{id}', 'index' => true, 'unique' => true, 'filter' => 'string' ], 'model_id' => [ 'label' => 'Model', 'type' => 'select_db', 'table' => 'device_models', 'options' => ['id', 'model'], 'index' => true, 'show' => ['model', 'model_name'] ], 'notes' => [ 'label' => 'Notes', 'type' => 'editor' ], 'category' => [ 'label' => 'Category', 'type' => 'select', 'form' => false, 'options' => [ 'individual' => 'Individual', 'company' => 'Company' ], 'classes' => [], 'required' => true, 'attributes' =>[], 'validations' => 'required', ],
Model Configuration
There's some additional properties we need to specify in our model.
-
$baseUrlThe route for the model. This will be used to generate create/view/edit/delete url later.
-
$entityNameName of the model's entity in singular form. Used for generating titles for various pages. e.g. Add New Product, View All Products
-
$entityNamePluralBy default, the
$entityNamewill be passed through laravel'sstr_pluralfunction to generate plural form of the entity name. If you want to override this and set custom plural form, you can use this property. -
$actionsWhen generating rows on a list page, by default, view, edit, delete these action links will be generated.But if you want to add extra action links for custom routes, you can add them in
$actionsarray. e.g. you want to add sale link for all your products in product list page, you can add:protected $actions = [ 'Sale' => '{url}/{id}/sale' ];
{url}will be replaced with the route and{id}will be replaced for that specific resource's id -
$pathsFor the fields which holds path for any type of assets e.g. images, documents etc. you can add their paths in a
$pathsarray. This will be handy when uploading/retrieving assets later.protected $paths = [ 'client_photo' => 'uploads/client/client_photo' ];