【问题标题】:SQL to JSON, ORM for PHP 7 and LaravelSQL 到 JSON,ORM 用于 PHP 7 和 Laravel
【发布时间】:2018-06-15 09:06:59
【问题描述】:

我正在开发一个 REST API,我必须在其中查询信息并返回正确的 JSON 对象。我正在寻找可以轻松完成这项工作的 PHP/Laravel 库。

一个带有表 JOIN 的普通 SQL 查询为我提供了多行信息

SQL:

    SELECT H.fname AS `fname`,
    H.sname AS `sname`,
    S.id AS `students.id`,
    A.topic AS `students.tasks.topic`,
    A.wordcount AS `students.tasks.wordcount`
    FROM `humans` AS `H`
    LEFT JOIN `students` AS `S` ON H.id = S.human
    LEFT JOIN `courses` AS `C` ON S.course = C.id
    LEFT JOIN `tasks` AS `T` ON T.student = S.id
    LEFT JOIN `assignments` AS `A` ON T.assignment = A.id
    WHERE H.email = "user@email.com"

以 JSON 编码的响应行:

[
    {
        "fname": "Donnie",
        "sname": "Ashok",
        "students.id": "987152e7b3056f1b96c964614b0b6328",
        "students.tasks.topic": "How to dismantle an Anti-matter injector manifold? ",
        "students.tasks.wordcount": 1500
    },
    {
        "fname": "Donnie",
        "sname": "Ashok",
        "students.id": "987152e7b3056f1b96c964614b0b6328",
        "students.tasks.topic": "How to intercept sub-space transmission using Dilithium Matrices? ",
        "students.tasks.wordcount": 1500
    },
    {
        "fname": "Donnie",
        "sname": "Ashok",
        "students.id": "3ea4f7b2e1e4bcc26ae544dd14d107fe",
        "students.tasks.topic": null,
        "students.tasks.wordcount": null
    }
]

但是,我需要像这样的适当 JSON 对象:

[
    {
        "fname": "Donnie",
        "sname": "Ashok",
        "students": [
            {
                "id": "987152e7b3056f1b96c964614b0b6328",
                "tasks": [
                    {
                        "topic": "How to intercept sub-space transmission using Dilithium Matrices? ",
                        "wordcount": 1500
                    },
                    {
                        "topic": "How to dismantle an Anti-matter injector manifold?",
                        "wordcount": 1500
                    }
                ]
            },
            {
                "id": "3ea4f7b2e1e4bcc26ae544dd14d107fe",
                "tasks": []
            }
        ]
    }
]

是否有任何 PHP 库或 ORM 可用于组织 SQL 行?

【问题讨论】:

  • 如果你不使用普通的 SQL 语句,你可以用 Laravel Eloquent 来做。 Eloquent 会以你想要的方式返回结果,或者你可以使用 Fractal : fractal.thephpleague.com/simple-example ,这可能会满足你想要的结果。
  • 使用空间分形。
  • jsoneditoronline.org/?id=2cb5e4c4375e4291ad87cd9583ae69e8 这是分形的结果。由于我需要深度较高的 JSON 数据,这似乎不是我所需要的。我会试试 Eloquent,有什么可以帮助我入门的推荐吗?
  • 使用 Laravel 5.5 资源创建你自己的 {JSON:API} 格式的 API 在 Laravel 5.5 中,我们现在有资源类,我们可以开箱即用地用于我们的 API,而无需安装任何第三方包。使用 eloquent Resource 类是一种将数据从一种格式转换为另一种格式的方法。

标签: php json laravel orm


【解决方案1】:

我使用JSON API API 规范,我认为它很好用。这是一个常用的规范。 要实现一个可以使用nilportugues/laravel5-jsonapi 包。

也请查看GraphQL。它是 API 的查询语言。 Folkloreatelier/laravel-graphql 是一个用于实现 GraphQL 的包。

这两者都会让你的 API 数据格式清晰。

JsonAPI 示例数据

{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "To TDD or Not"
    }
  }
}

GraphQL 示例数据

{
  "data": {
    "human": {
      "name": "Luke Skywalker",
      "height": 1.72
    }
  }
}

正如 cmets php 联盟中的其他人所提到的,fractal 也是一个很好的选择。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2011-03-21
    • 2022-01-22
    • 2018-03-31
    • 2022-11-02
    相关资源
    最近更新 更多