🔁 Automatically Map Request Keys with Laravel Case Mapper Request

Er Amit Gupta

Er Amit Gupta

2025-07-29T09:12:27Z

3 min read

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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',
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

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,
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

⚡ 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>
Enter fullscreen mode Exit fullscreen mode

📥 Incoming Request:

{
  "firstName": "Amit",
  "lastName": "Gupta",
  "emailAddress": "amit@example.com"
}
Enter fullscreen mode Exit fullscreen mode

✅ Automatically Mapped To:

[
  'first_name' => 'Amit',
  'last_name' => 'Gupta',
  'email_address' => 'amit@example.com'
]
Enter fullscreen mode Exit fullscreen mode

🔧 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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Then use it like this:

#[MapName(KebabCaseMapper::class)]
Enter fullscreen mode Exit fullscreen mode

✅ 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
Enter fullscreen mode Exit fullscreen mode

📘 GitHub Repoeramitgupta/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!