extraFields方法

指定由于终端用户的请求包含 expand 参数哪些额外的字段应被包含到展现数组,例如

// 返回 fields() 方法中声明的所有字段,以及 extraFields() 方法中的“profile”字段
http://localhost/users?expand=profile

// 返回 fields() 方法中声明的所有字段,以及 post 中的“author”
// 如果它位于 post 模型的 extraFields() 中
http://localhost/comments?expand=post.author

// 返回 fields() 方法中的“id”,“email”,以及 extraFields() 方法中的“profile”字段
http://localhost/users?fields=id,email&expand=profile

extraFields() 返回的数据格式和 fields() 相同, 一般 extraFields() 主要用于指定哪些值为对象的字段, 例如,给定以下字段申明

public function extraFields()
{
    return ['profile'];
}

http://localhost/users?fields=id,email&expand=profile 的请求可能返回如下 JSON 数据:

[
    {
        "id": 100,
        "email": "100@example.com",
        "profile": {
            "id": 100,
            "age": 30,
        }
    },
    ...
]