在 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
[ { "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
[ { "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 } ]
近期评论