没有不值得去解决的问题,也没有不值得去学习的技术!

Converting Between CamelCase API Responses and snake_case Database Columns in Laravel 6

View the generated raw SQL statement in the logs

1. The table field names use lowercase letters with underscores. See Figure 1.

Table field names use lowercase letters with underscores
Figure 1

2. The API is implemented based on GraphQL, and the specification requires that the field names in the API response use camelCase.

GraphQL

type ThemeAsset
{
    "ID"
    id: ID!
    "主题ID"
    themeId: String!,
    "版本"
    version: String!,
    "内容"
    content: String,
    "路径,相对于主题的路径,如 ages/index.blade.php"
    key: String!,
    "MIME 类型"
    mimeType: String!,
    "类别,page、component、config(仅这3种类型有配置),asset、unknown"
    category: String!,
    "主题的 Schema,包括页面、主题、组件"
    schema: String,
    "创建时间"
    createdAt: DateTime!
    "更新时间"
    updatedAt: DateTime
}

3. Print $themeAssets in the method of the resolver /Modules/ThemeStore/Resolver/OnlineStoreTheme/ThemeAssetsResolver.php, and the object property names use lowercase letters with underscores. See Figure 2.

Print $themeAssets in the method of the resolver /Modules/ThemeStore/Resolver/OnlineStoreTheme/ThemeAssetsResolver.php, and the object property names use lowercase letters with underscores
Figure 2
PHP
<?php
 
namespace Modules\ThemeStore\Resolver\OnlineStoreTheme;
 
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use GraphQL\Type\Definition\ResolveInfo;
use Modules\ThemeStore\Entities\ThemeAsset;
 
class ThemeAssetsResolver
{
    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $themeId = $rootValue['themeAssets']['theme_id'];
 
        $themeAssets = ThemeAsset::where('theme_id', $themeId)->get();
 
        print_r($themeAssets);
        exit;
 
    }
}

4. Option 1 is to iterate over $themeAssets using a foreach loop, assign the values to a new array, and set the keys to camelCase in this array. Option 2 is to rename the fields to camelCase directly in the select query statement. I ultimately decided to adopt Option 2.

PHP

    public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
    {
        $themeId = $rootValue['themeAssets']['theme_id'];

        $themeAssets = ThemeAsset::where('theme_id', $themeId)
            ->select(
                'id',
                'theme_id as themeId',
                'version',
                'content',
                'asset_key as key',
                'mime_type as mimeType',
                'category',
                'schema',
                'created_at as createdAt',
                'updated_at as updatedAt'
            )
            ->get();

        return $themeAssets;
    }

5. Request the API, and the response field names are in camelCase. This meets expectations. See Figure 3.

Request the API, and the response field names are in camelCase. This meets expectations
Figure 3

6. View the generated raw SQL statement in the logs. See Figure 4.

View the generated raw SQL statement in the logs
Figure 4
SQL

select
  `id`,
  `theme_id` as `themeId`,
  `version`,
  `content`,
  `asset_key` as `key`,
  `mime_type` as `mimeType`,
  `category`,
  `schema`,
  `created_at` as `createdAt`,
  `updated_at` as `updated_at`
from
  `theme_asset`
where
  `theme_id` = 'vogue'

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系