🔁 Automatically Map Request Keys with Laravel Case Mapper Request

Er Amit Gupta
2025-07-29T09:12:27Z
Handling inconsistent key naming from various frontends can be a real headache. Whether it’s camelCase, snake_case, StudlyCase, or even UPPERCASE — mismatched key formats often lead to unnecessary data transformation or validation errors.
Laravel developers, meet your new best friend:
Laravel Case Mapper Request
— a zero-configuration Laravel package that automatically maps incoming request keys to match your backend validation rules using PHP 8+ Attributes.
🎯 Why You Need This
Modern APIs interact with various clients — React, Vue, Angular, mobile apps — all of which may send form data in different key formats. But your Laravel backend expects consistent keys, especially when using FormRequest
validation.
Instead of writing custom logic to reformat input keys every time, let Laravel Case Mapper Request handle it automatically using clean, expressive attributes.
✨ Key Features
- ✅ Attribute-based key mapping using
#[MapName(...)]
- 🔄 Supports multiple naming conventions:
snake_case
,camelCase
,StudlyCase
,UPPERCASE
- 🔧 Extensible with your own custom mappers
- 🎨 Works with any frontend (React, Vue, etc.)
- 📦 Zero configuration, zero hacks
- 🧩 Seamlessly integrates with Laravel's
FormRequest
💡 Works with Laravel 10, 11, and 12 — no core modification required.
⚙️ Installation
composer require erag/laravel-case-mapper-request
That’s it. No config file, no publishing needed. You’re ready to go.
🧩 How It Works — In 3 Simple Steps
1. Create a Form Request
php artisan make:request ContactRequest
2. Add the Mapper Attribute & Trait
use LaravelCaseMapperRequest\Attributes\MapName;
use LaravelCaseMapperRequest\Traits\HasKeyTransformers;
use LaravelCaseMapperRequest\Mappers\SnakeCaseMapper;
#[MapName(SnakeCaseMapper::class)]
class ContactRequest extends FormRequest
{
use HasKeyTransformers;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'first_name' => 'required|string',
'last_name' => 'required|string',
'email_address' => 'required|email',
];
}
}
3. Use the Form Request in a Controller
class ContactController extends Controller
{
public function store(ContactRequest $request)
{
$validated = $request->validated();
// Example: Contact::create($validated);
return response()->json([
'message' => 'Submitted successfully!',
'data' => $validated,
]);
}
}
⚡ Frontend Example (Vue + Inertia)
Let’s say your frontend sends data in camelCase
format:
<script setup>
import { useForm } from '@inertiajs/vue3'
const form = useForm({
firstName: '',
lastName: '',
emailAddress: ''
})
function submit() {
form.post('/form/snake')
}
</script>
📥 Incoming Request:
{
"firstName": "Amit",
"lastName": "Gupta",
"emailAddress": "amit@example.com"
}
✅ Automatically Mapped To:
[
'first_name' => 'Amit',
'last_name' => 'Gupta',
'email_address' => 'amit@example.com'
]
🔧 Built-in Case Mappers
Mapper | Input Format | Mapped Output |
---|---|---|
SnakeCaseMapper |
firstName |
first_name |
CamelCaseMapper |
first_name |
firstName |
StudlyCaseMapper |
FirstName |
first_name |
UpperCaseMapper |
FIRSTNAME |
firstname |
🛠️ Create Your Own Mapper
Need a custom casing logic like kebab-case
?
Just implement the CaseMapperContract
:
use LaravelCaseMapperRequest\Contracts\CaseMapperContract;
class KebabCaseMapper implements CaseMapperContract
{
public static function map(array $data): array
{
return collect($data)
->mapWithKeys(fn($value, $key) => [Str::kebab($key) => $value])
->toArray();
}
}
Then use it like this:
#[MapName(KebabCaseMapper::class)]
✅ Who Should Use This?
- Laravel backend developers dealing with multiple frontend teams
- Projects with mixed casing inputs (camelCase, snake_case, etc.)
- APIs that require flexible, clean, and consistent request data
- Teams looking to eliminate boilerplate request formatting logic
🧠 Final Thoughts
Laravel Case Mapper Request is a must-have for teams building robust APIs or multi-client Laravel apps. It simplifies input handling, reduces validation bugs, and keeps your code expressive and DRY — all using modern PHP 8+ attribute syntax.
No config. No hacks. Just clean input mapping.
📦 Install Now
composer require erag/laravel-case-mapper-request
📘 GitHub Repo → eramitgupta/laravel-case-mapper-request
🔗 Connect with the Author
Made with ❤️ by Amit Gupta
⭐ Found it useful?
Give it a ⭐ on GitHub and help others discover it too!