Download the PHP package stahiralijan/request-caster without Composer
On this page you can find all versions of the php package stahiralijan/request-caster. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download stahiralijan/request-caster
More information about stahiralijan/request-caster
Files in stahiralijan/request-caster
Package request-caster
Short Description This package intercepts Laravel Form Submits after successful validation and provides useful function and also casts the submitted form data accordingly e.g. Upper-case first Characters of Words, Capitalized them, and checkbox value "1" to boolean, JSON string to PHP Array conversions etc.
License MIT
Informations about the package request-caster
Laravel RequestCaster
Requirements: I've only tested this package with Laravel 5.5, please help me by testing this package in older versions of Laravel
Installation
Install this package by typing the following command:
Usage
Let's learn from an example:
You want to be able to save the submitted data but you don't want to make a mess in the controller method like this:
As you can see after a while you start to wondering what if there is a way you could automate this process so that your controller would look elegant and clean. With this package you can just do that:
Step 1:
Use the RequestCaster
Trait in your form request (in this case UserFormRequest
):
Step 2:
Define the Request attributes that are required to be casted:
Finally
...and that's all you needed to do, first_name
and last_name
are automatically capitalized. Also, you don't need to worry about your form data being getting dirty before validation because these castings will run after the validator validates the form data.
Available transformations / Casts
The following casts are available:
$toLowerCaseWords
: Appliesstrtolower()
to the selected field(s).$toUpperCaseWords
: Appliesstrtoupper()
to the selected field(s).$toUCFirstWords
: Appliesucwords()
to the selected field(s).$toSlugs
: Appliesstr_slug()
to the selected field(s).$toIntegers
: Casts selected field(s) toint
.$toFloats
: Casts selected field(s) tofloat
.$toBooleans
: Casts selected field(s) tobool
.$toArrayFromJson
: Appliesjson_decode()
to the selected fields.$joinStrings
: Joins two or more fields and sets the result in new field specified in the array key, syntax:$joinStrings = ['newField' => 'glue|field1,field2,...,fieldn']
Available methods
collection(array $keys)
returns an object ofIlluminate\Support\Collection
dd()
dumps and dies all the fields submitted in requestdump()
dumps all the fields submitted in request
You can use this method to get a collection (Illuminate\Support\Collection
) of all the attributes
How to cast
All of the properties are pretty straight forward, you define the attributes that needs to be casted like this:
You got the idea about the usage of the simple stuff, now one special transformation / caster
- Here
fullname
will be a new attribute of theFormRequest
which does not exists in the either the form or the FormRequest in the current context. - Notice a space
' '
in the starting of value is the glue of the two attributes - Next
|
is the separator between the glue and the desired attributes - Next you add the attributes that needs to be glued.
If first_name
is Tahir
and last_name
is Jan
the output will be Tahir Jan
according to the above rule, and can be accessed with $request->fullname
or $request->get('fullname')