shetabit / transform-request
Transform laravel requests
Requires
- php: ^8.4
- illuminate/console: ^12.0|^13.0
- illuminate/http: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- shetabit/transformer: ^2.0.1|^3.0
Requires (Dev)
- larastan/larastan: ^3.10
- orchestra/testbench: ^10.0|^11.0
- phpcsstandards/php_codesniffer: ^4.0
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^11.5|^12.0|^13.0
- rector/rector: ^2.6
This package is auto-updated.
Last update: 2026-08-17 07:45:25 UTC
README
Transform laravel requests
you can normalize or change request data structure with transformers.
This package supports PHP 8.4+ and Laravel 12 and 13.
lets normalize our data in
transformersand letcontrollersto be much more cleaner and smaller.
List of contents
Install
Via Composer
$ composer require shetabit/transform-request
How to use
Create a new data transformer
we use transformers to transform request data.
you can run the below command in your console in order to create a new data transformer named TestTransformer.
$ composer php artisan make:transformer TestTransformer
all transformers will be created in App\Http\Transformers path.
Transformer example:
in all transformers, the transform method will transform your data into your ideal one.
for example we can write the below code in it:
namespace App\Http\Transformers; use Shetabit\Transformer\Contracts\TransformerInterface; class TestTransformer implements TransformerInterface { /** * transform given data * * @param array $data * @return array */ public function transform(array $data) : array { /* input data : [ 'n' => 'mahdi', 'f' => 'khanzadi' ] transformed data: [ 'name' => 'mahdi', 'family' => 'khanzadi', 'username' => 'mahdikhanzadi' ] */ return [ 'name' => $data['n'] ?? null, 'family' => $data['f'] ?? null, 'username' => ($data['n'] ?? null).($data['f'] ?? null) ]; } }
Transform requests
we can use a transformer to transform requests like the below:
namespace App\Http\Controllers; use App\Http\Transformers\TestTransformer; use Illuminate\Http\Request; class TestController extends Controller { public function __invoke(Request $request) { $data = $request->transform()->get(new TestTransformer()); print_r($data) } }
Change log
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email khanzadimahdi@gmail.com instead of using the issue tracker.
Credits
Testing
Every pull request and every push to master is checked by GitHub Actions: the test suite runs on
PHP 8.4 and 8.5 against Laravel 12 and 13 (both the lowest and the highest supported dependencies), the coding style
is checked with PHP_CodeSniffer, the sources are analysed with PHPStan and the code coverage is measured.
The feature tests send real requests through a Laravel application built by Orchestra Testbench, and the
make:transformer command is run and its output checked.
composer install composer test # run the test suite composer test-coverage # run the test suite and report code coverage composer check-style # check the coding style composer fix-style # fix the coding style where possible composer analyse # run static analysis composer ci # run all of the checks above
If you would rather not install PHP on your machine, the shipped Dockerfile and Makefile run everything inside a
container:
make test # run the test suite make coverage # run the test suite and report code coverage make check-style # check the coding style make fix-style # fix the coding style where possible make analyse # run static analysis make ci # run all of the checks above make shell # open a shell inside the container make help # list every available target
Another PHP version can be used with make test PHP_VERSION=8.5.
License
The MIT License (MIT). Please see License File for more information.
