# 转换结果

我们已经介绍了对结果的排序跟过滤，现在我们来讨论Eloquent的JSON序列化，这意味着我们可以返回模型的每个字段。

Eloquent提供了一些方便的工具用于当你序列化一个数组是要显示那些字段。你可以在第五章阅读更多，但要点是，如果你在Eloquent类设置了一个$hidden数组属性，则该数组列出的字段都不会显示在序列化输出中。你也可以设置一个$visible数组来定义哪些字段用于显示，你还可以覆盖模型上的toArray()数组来定制输出格式。

另外一种常见的模式是为每种数据类型创建转换器。转换器是很有帮助的，因为他们赋予了你更多的控制权，它将API的逻辑与模型隔离开来，并允许你提供一个更一直的API，即使模型以及他们的关系变更了也是如此。

这是一个奇妙而复杂的包，Fractal，它为一系列便利结构和类提供了转换数据的方法。

Laravel 5.5引入了一个称为API 资源的概念，它涵盖了大多数API转换和收集结果相关内容。如果你使用的是5.5及之后的版本，跳过下面的章节直接跳到第352页的"API Resources",如果你使用的是5.4或更早版本，请继续阅读。

译者注:因为我们当前的版本是5.8所以就直接跳到API Resources吧


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liseen315.gitbook.io/laravel/bian-xie-apis/zhuan-huan-jie-guo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
