rizkussef/laravel-snake-to-camel-case 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rizkussef/laravel-snake-to-camel-case

最新稳定版本:v1.0.8

Composer 安装命令:

composer require 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

README 文档

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!

统计信息

  • 总下载量: 217
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-21