erdemozveren / laravelmacros
Laravel helper macros for your project
Requires
- php: ^7.1.3
- laravelcollective/html: ~6.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3.0
- phpunit/phpunit: ^7.0
- sempro/phpunit-pretty-print: ^1.0
README
I'm lazy,so i made package with some handful shourtcuts for Laravel.
Compatibility:Laravel 5.8+ , Not stable for production
Usage
Installation
Via Composer
$ composer require erdemozveren/laravelmacros
For customization, first publish config file
$ php artisan vendor:publish --provider="erdemozveren\laravelmacros\LaravelMacrosServiceProvider" --tag=config
Now you can edit default configuration for inputs,settings etc.
Getting Started
laravelmacros
aims to build simple skeleton for forms and form proccess easy
To getting started add in your model use erdemozveren\laravelmacros\Traits\FormBuilderTrait;
then in class use it as trait use FormBuilderTrait;
now you ready to continue!
this package add Laravel collective macros to get things easier,you can use it by just simply add "c" to start and Upper case next letter,like this;
Form::text('email', 'example@gmail.com') --> Form::cText('email', 'example@gmail.com')
Usage
Form Builder
add formFields function to model
public function formFields() { return [ "*"=>[ // wildcard will be applied to all elements "options"=> ["style"=>"color:red!important"] ], "parent_id"=>[ // "parent_id" is the name attribute "type"=>"select", // input type (e.g. "select" will look for "cSelect") "label"=>"Parent", // label text "data"=>User::pluck('full_name','id'), // [ONLY FOR select] you can write any data source that laravel collective accepts "options"=>[ // optional "required"=>false // optional make input optional // ... and other "laravel collective" options and dom parameters can be used in here ] ], "full_name"=>[ "type"=>"text", "label"=>"Full Name", ], "email"=>[ "type"=>"text", "label"=>"E-mail", ], "password"=>[ "type"=>"password", "label"=>"Password", ], ]; }
Generate Form
In your blade file you can use like this
{!!Form::model($model,['url'=>"/post"])!!} {!!$model->generateForm()!!} {!!Form::cSubmit()!!} {!!Form::close()!!}
or
{!!Form::open(['url'=>"/post"])!!} {!!$model->generateForm()!!} {!!Form::cSubmit()!!} {!!Form::close()!!}
if you want to exclude one or more elements in one form just pass option _exclude
with array of fields name
{!!$model->generateForm(["_exclude"=>["full_name","another_filed_name"]])!!}
Auto generate form fields
$ php artisan laravelmacros:formfields {tablename}
it will ask you some questions then give you formFields
function code to copy into model file
Form Macros
These macros extended from laravel collective
and can be used in model binding too.
Value of the input will be (in order) ;
- Session Flash Data (Old Input)
- Data From Current Request (via
Request::input
method) - Explicitly Passed Value
- Model Attribute Data
// variable names stand for paramaters with default values Form::cText($name,$label,$placeholder=null,$options=[]); Form::cColor($name,$label,$options=[]); Form::cTextarea($name,$label,$placeholder=null,$options=[]); Form::cNumber($name,$label,$options=[]); Form::cEmail($name,$label,$placeholder=null,$options=[]); Form::cPassword($name,$label,$placeholder="*******",$options=[]); Form::cFile($name,$label,$options=[]); Form::cCheckbox($name,$label,$value=1,$checked=null); Form::cRadio($name,$label,$value,$checked=null); Form::cSubmit($label="Submit",$class=""); Form::cSelect($name,$label,$data,$options=[]);
Change log
Please see the changelog for more information on what has changed recently.
Contributing
Please see contributing.md for details and a todolist.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
MIT