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

在 Laravel 6 的 Eloquent 集合中,调整 API 响应结构的嵌套层级

接口响应结构如下
1、现有的代码实现如下


$wpThemes = Theme::with('themeInstallation.themeInstallationVersionPreset.themeInstallationTasks')->withCount(['logs'])->orderBy('updated_at_gmt', 'desc')->get()->makeHidden('content');
return $wpThemes;


2、接口响应结构如下,如图1
接口响应结构如下
图1


[
    {
        "id": 104,
        "custom_name": "xxx",
        "name": "xxx",
        "is_original": false,
        "is_default": false,
        "created_at_gmt": "2022-05-27 01:43:18",
        "updated_at_gmt": "2022-05-27 01:43:18",
        "original_theme_name": "default",
        "logs_count": 0,
        "created_at": "2022-05-27 09:43:18",
        "updated_at": "2022-05-27 09:43:18",
        "cover_url": "https:\/\/xxx-s3.s3.us-east-2.amazonaws.com\/defaults\/xxx.jpg",
        "alias": "xxx",
        "architecture": "2.0",
        "is_fission": false,
        "theme_installation": {
            "id": 2,
            "theme_store_theme_id": 9,
            "theme_id": "96653e3a-9f95-44dd-ade9-d928a59d0332",
            "wp_theme_id": 104,
            "theme_name": "xxx",
            "theme_custom_name": "xxx",
            "role": "unpublished",
            "processing": 0,
            "processing_failed": 1,
            "created_at": "2022-05-27 09:43:18",
            "updated_at": "2022-05-27 09:43:22",
            "theme_installation_version_preset": {
                "id": 2,
                "theme_installation_id": 2,
                "theme_store_theme_id": 9,
                "theme_store_theme_version_id": 1,
                "theme_store_theme_version_preset_id": 1,
                "theme_store_theme_version_preset_zip_url": "https:\/\/xxx.s3.xxx.amazonaws.com\/xxx\/xxx.zip",
                "update_state": "manual_update",
                "processing": 0,
                "processing_failed": 1,
                "created_at": "2022-05-27 09:43:18",
                "updated_at": "2022-05-27 09:43:22",
                "theme_installation_tasks": [
                    {
                        "id": 2,
                        "theme_installation_id": 2,
                        "theme_installation_version_preset_id": 2,
                        "processing": 0,
                        "processing_failed": 1,
                        "step": 4,
                        "failed_message": "file_get_contents(E:\\wwwroot\\xxx\\platform\\storage\\app\/theme_downloads\/2022\/05\/27\/1653615799.0006.1605685643\\.themeignore): failed to open stream: No such file or directory",
                        "created_at": "2022-05-27 09:43:18",
                        "updated_at": "2022-05-27 09:43:22"
                    }
                ]
            }
        }
    }
]


3、期望将字段 theme_installation 中的一些字段放入第一级中,即与 id 平级,最后删除掉 theme_installation。 4、最终实现代码如下


        $wpThemes = Theme::with('themeInstallation.themeInstallationVersionPreset.themeInstallationTasks')->withCount(['logs'])->orderBy('updated_at_gmt', 'desc')->get()->makeHidden('content');
        $themes = [];
        foreach ($wpThemes as $wpTheme) {
            $wpTheme['role'] = $wpTheme->themeInstallation['role'] ?? ThemeInstallation::ROLE_UNPUBLISHED;
            $wpTheme['processing'] = $wpTheme->themeInstallation['processing'] ?? ThemeInstallation::PROCESSING_FALSE;
            $wpTheme['processing_failed'] = $wpTheme->themeInstallation['processing_failed'] ?? ThemeInstallation::PROCESSING_FAILED_FALSE;
            $wpTheme['theme_store_theme_id'] = $wpTheme->themeInstallation['theme_store_theme_id'] ?? 0;
            $wpTheme['theme_id'] = $wpTheme->themeInstallation['theme_id'] ?? '';
            $wpTheme['installation_task_id'] = $wpTheme->themeInstallation->themeInstallationVersionPreset->themeInstallationTasks[0]['id'] ?? 0;
            $wpTheme['installation_task_failed_message'] = $wpTheme->themeInstallation->themeInstallationVersionPreset->themeInstallationTasks[0]['failed_message'] ?? '';
            unset($wpTheme->themeInstallation);
            $themes[] = $wpTheme;
        }
        return $themes;


5、最终的响应结构如下,符合预期。如图2
最终的响应结构如下,符合预期
图2


[
    {
        "id": 104,
        "custom_name": "xxx",
        "name": "xxx",
        "is_original": false,
        "is_default": false,
        "created_at_gmt": "2022-05-27 01:43:18",
        "updated_at_gmt": "2022-05-27 01:43:18",
        "original_theme_name": "default",
        "logs_count": 0,
        "role": "unpublished",
        "processing": 0,
        "processing_failed": 1,
        "theme_store_theme_id": 9,
        "theme_id": "96653e3a-9f95-44dd-ade9-d928a59d0332",
        "installation_task_id": 2,
        "installation_task_failed_message": "file_get_contents(E:\\wwwroot\\xxx\\platform\\storage\\app\/theme_downloads\/2022\/05\/27\/1653615799.0006.1605685643\\.themeignore): failed to open stream: No such file or directory",
        "created_at": "2022-05-27 09:43:18",
        "updated_at": "2022-05-27 09:43:18",
        "cover_url": "https:\/\/xxx-s3.s3.us-east-2.amazonaws.com\/defaults\/xxx.jpg",
        "alias": "xxx",
        "architecture": "2.0",
        "is_fission": false
    }
]


PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理