rizkussef/laravel-snake-to-camel-case

A Laravel middleware package to automatically convert API response keys from snake_case to camelCase for frontend compatibility, and optionally convert request keys from camelCase to snake_case

Installs: 198

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/rizkussef/laravel-snake-to-camel-case

v1.0.8 2025-07-22 10:48 UTC

This package is auto-updated.

Last update: 2025-12-22 11:48:37 UTC


README

Packagist Version Total Downloads License Install with Composer

✨ What is this?

A Laravel package that automatically converts API keys between snake_case and camelCase:
Incoming Requests: Converts camelCasesnake_case (for Eloquent models & DB consistency).
Outgoing Responses: Converts snake_casecamelCase (for React, Vue, or any frontend apps).

Perfect for clean API responses and JavaScript-friendly keys! 🚀

✅ Features

  • 🔄 Two-way conversion: Request & Response.

  • 🔍 Works on nested arrays & JSON.

  • 🔗 Applies to all routes or only API routes (configurable).

  • Plug & play using Laravel’s package auto-discovery.

  • 🛠 Fully configurable via config/snake-to-camel.php.

📦 Installation

Require the package via Composer:

composer require rizkussef/laravel-snake-to-camel-case

⚙️ Configuration

Publish the config file:

php artisan vendor:publish --tag=config

Then edit config/snake-to-camel.php:

return [
    'convert_response' => true, // Enable snake_case → camelCase for responses
    'convert_request'  => true, // Enable camelCase → snake_case for requests
    'apply_to'         => 'all', // Options: 'all' | 'api' | 'web'
];

✅ You can also override these settings in .env:

SNAKE_TO_CAMEL_CONVERT_RESPONSE=true
SNAKE_TO_CAMEL_CONVERT_REQUEST=true

🔍 How It Works

✅ Example Request (Frontend → Laravel)

Before:

{
    "firstName": "Rizk",
    "lastName": "Ussef"
}

After Middleware (Laravel receives):

{
    "first_name": "Rizk",
    "last_name": "Ussef"
}

✅ Example Response (Laravel → Frontend)

Before:

{
    "first_name": "Rizk",
    "last_name": "Ussef"
}

After Middleware (Frontend receives):

{
    "firstName": "Rizk",
    "lastName": "Ussef"
}

🏗 Usage

No manual registration needed. Laravel auto-discovers the service provider:
Rizkussef\LaravelSnakeToCamelCase\Providers\SnakeToCamelServiceProvider

The middleware automatically applies to:

  • ✅ All routes (if apply_to = all)

  • ✅ Only API routes (if apply_to = api)

📌 Requirements

  • PHP 8.1+

  • Laravel 10.x | 11.x

📄 License

This package is open-source under the MIT license.
See LICENSE for details.

⭐ Support

If you like this package, please star the repo on GitHub
Contributions and PRs are welcome!