Methods to allow the mapping of cases to snake / camel for input / output
phpsa/laravel-case-remapping is a Laravel package for methods to allow the mapping of cases to snake / camel for input / output.
It currently has 4 GitHub stars and 25 downloads on Packagist (latest version v1.0.1).
Install it with composer require phpsa/laravel-case-remapping.
Discover more Laravel packages by phpsa
or browse all Laravel packages to compare alternatives.
Last updated
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
You can install the package via composer:
composer require phpsa/laravel-case-remapping
Add as a middleware:
Phpsa\LaravelCaseRemapping\Http\Middleware\SnakeCaseInputs to your route eg:
Route::post('xxx',[...])->withMiddleware(\Phpsa\LaravelCaseRemapping\Http\Middleware\SnakeCaseInputs::class)
in your controllers constructor:
public function __construct()
{
$this->middleware(\Phpsa\LaravelCaseRemapping\Http\Middleware\SnakeCaseInputs::class);
}
Or globally via the app/Http.Kernal.php file
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Phpsa\LaravelCaseRemapping\Http\Middleware\SnakeCaseInputs::class
],
];
add the following trait to your resource:
\Phpsa\LaravelCaseRemapping\Http\Resources\WithAcceptedCase
then in your toArray method change to per example
public function toArray($request){
$data = parent::toArray($request);
... // any other modifications
return $this->toAcceptCase($request, $data);
}
based on the header value for X-Accept-Case-Type passed to the request it will reaturn one of the following
camel, kebab, snake
This packages includes 3 collecion macros:
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.