使用 Laravel 为 React-Native 应用程序提供 json 响应

发布于 2025-01-20 16:47:34 字数 1667 浏览 1 评论 0原文

有人告诉我,我们在前端有一些限制,我们不能使用正常的 Eloquent json 响应,相反,我应该准备如下响应:

[
    {
        "title": null,
        "type": "animals",
        "description": null,
        "items": [
            {
                "type": "animal",
                "content": {
                    "id": 31,
                    "image": null,
                    "name": "Asghar's dog",
                    "age": null,
                    "race": null,
                    "species_id": 1,
                    "reason": [
                        "Home"
                    ]
                }
            }
        ]
    },
    {
        "title": "Recommend",
        "type": "recommendations",
        "description": null,
        "items": [
            {
                "type": "self_checks",
                "content": {
                    "type": "prompt",
                    "title": "Self check for Asghar's dog.",
                    "description": "A short Self check.",
                    "cta_text": "Los geht’s"
                }
            },
            {
                "type": "worm_checks",
                "content": {
                    "title": "Wurm-Vorsorge für Asghar's dog.",
                    "description": "Finde heraus, wie hoch das Wurm-Risiko deiner Fellnase ist.",
                    "cta_text": "Los geht’s",
                    "health_check_payload": {
                        "url": "https://****.de/check/worms?utm_term=Worms&utm_content=primer",
                        "title": "Wurm Check"
                    }
                }
            }
        ]
    }
]

我需要知道应该如何构建我的代码以避免代码重复并保留它干净的。另外,我正在寻找有关此类回复的任何文件。

I've been told that we have some limitations in front end side and we can't use the normal Eloquent json responses, instead I should prepare responses like:

[
    {
        "title": null,
        "type": "animals",
        "description": null,
        "items": [
            {
                "type": "animal",
                "content": {
                    "id": 31,
                    "image": null,
                    "name": "Asghar's dog",
                    "age": null,
                    "race": null,
                    "species_id": 1,
                    "reason": [
                        "Home"
                    ]
                }
            }
        ]
    },
    {
        "title": "Recommend",
        "type": "recommendations",
        "description": null,
        "items": [
            {
                "type": "self_checks",
                "content": {
                    "type": "prompt",
                    "title": "Self check for Asghar's dog.",
                    "description": "A short Self check.",
                    "cta_text": "Los geht’s"
                }
            },
            {
                "type": "worm_checks",
                "content": {
                    "title": "Wurm-Vorsorge für Asghar's dog.",
                    "description": "Finde heraus, wie hoch das Wurm-Risiko deiner Fellnase ist.",
                    "cta_text": "Los geht’s",
                    "health_check_payload": {
                        "url": "https://****.de/check/worms?utm_term=Worms&utm_content=primer",
                        "title": "Wurm Check"
                    }
                }
            }
        ]
    }
]

I need to know how should I structure my code to avoid code repetition and keep it clean. Also, I'm looking for any document regarding this type of responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文