Laravel5.5中如何通过API Resources高效生成定制化JSON输出?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1680个文字,预计阅读时间需要7分钟。
自Laravel 5.5起,我们引入了API Resources这一概念。简单来说,这是在构建API时,位于Eloquent模型和JSON响应之间的一个转换层。例如,官方文档中这样描述:在构建API时,你可能需要一个位于你的Eloquent模型和JSON响应之间的转换层。
从Laravel 5.5+开始,加入了API Resources这个概念。
我们先来看一下官网如何定义这个概念的:
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
可能看完这个概念之后,你仍然有点不明白,毕竟这个定义说的有点含糊。
本文共计1680个文字,预计阅读时间需要7分钟。
自Laravel 5.5起,我们引入了API Resources这一概念。简单来说,这是在构建API时,位于Eloquent模型和JSON响应之间的一个转换层。例如,官方文档中这样描述:在构建API时,你可能需要一个位于你的Eloquent模型和JSON响应之间的转换层。
从Laravel 5.5+开始,加入了API Resources这个概念。
我们先来看一下官网如何定义这个概念的:
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.
可能看完这个概念之后,你仍然有点不明白,毕竟这个定义说的有点含糊。

