<article><h1>Eloquent: 序列化</h1><ul><li><a href="#introduction">简介</a></li><li><a href="#serializing-models-and-collections">序列化模型 & 集合</a><ul><li><a href="#serializing-to-arrays">序列化成数组</a></li><li><a href="#serializing-to-json">序列化成 JSON</a></li></ul></li><li><a href="#hiding-attributes-from-json">隐藏来自 JSON 的属性</a></li><li><a href="#appending-values-to-json">添加参数到 JSON 中</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p>当你在创建 JSON API 的时候,经常会需要将模型和关联转换成数组或 JSON。Eloquent 提供了一些便捷的方法来让我们可以完成这些转换,以及控制哪些属性需要被包括在序列化中。</p><p><a name="serializing-models-and-collections"></a></p><h2><a href="#serializing-models-and-collections">序列化模型 & 集合</a></h2><p><a name="serializing-to-arrays"></a></p><h3>序列化成数组</h3><p>如果要将模型还有其加载的 <a href="/docs/5.4/eloquent-relationships">关联</a> 转换成一个数组,则可以使用 toArray 方法。这个方法是递归的,因此,所有属性和关联(包含关联中的关联)都会被转换成数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">with<span class="token punctuation">(</span></span><span class="token string">'roles'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>你也可以将整个 <a href="/docs/5.4/eloquent-collections">集合</a> 转换成数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$users</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="serializing-to-json"></a></p><h3>序列化成 JSON</h3><p>如果要将模型转换成 JSON,则可以使用 <code class=" language-php">toJson</code> 方法。如同 <code class=" language-php">toArray</code> 方法一样,<code class=" language-php">toJson</code> 方法也是递归的。因此,所有的属性以及关联都会被转换成 JSON:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toJson<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>或者,你也可以强制把一个模型或集合转型成一个字符串,它将会自动调用 <code class=" language-php">toJson</code> 方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">find<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>string<span class="token punctuation">)</span> <span class="token variable">$user</span><span class="token punctuation">;</span></code></pre><p>当模型或集合被转型成字符串时,模型或集合便会被转换成 <code class=" language-php"><span class="token constant">JSON</span></code> 格式,因此你可以直接从应用程序的路由或者控制器中返回 <code class=" language-php">Eloquent</code> 对象:</p><pre class=" language-php"><code class=" language-php"><span class="token scope">Route<span class="token punctuation">::</span></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token scope">App<span class="token punctuation">\</span>User<span class="token punctuation">::</span></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="hiding-attributes-from-json"></a></p><h2><a href="#hiding-attributes-from-json">隐藏来自 JSON 的属性</a></h2><p>有时候你可能会想要限制包含在模型数组或 JSON 表示中的属性,比如说密码。则可以通过在模型中增加 <code class=" language-php"><span class="token variable">$hidden</span></code> 属性定义来实现:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 在数组中想要隐藏的属性。
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$hidden</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'password'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 当你要对关联进行隐藏时,需使用关联的 <strong>方法</strong> 名称,而不是它的动态属性名称。</p></blockquote><p>另外,你也可以使用 <code class=" language-php">visible</code> 属性来定义应该包含在你的模型数组和 JSON 表示中的属性白名单。白名单外的其他属性将隐藏,不会出现在转换后的数组或 JSON 中:</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 在数组中可见的属性。
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$visible</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'first_name'</span><span class="token punctuation">,</span> <span class="token string">'last_name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><h4>临时修改属性的可见度</h4><p>你可以在模型实例后使用 <code class=" language-php">makeVisible</code> 方法来显示通常隐藏的属性,且为了便于使用,<code class=" language-php">makeVisible</code> 方法会返回一个模型实例:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">makeVisible<span class="token punctuation">(</span></span><span class="token string">'attribute'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>相应的,你可以在模型实例后使用 <code class=" language-php">makeHidden</code> 方法来隐藏通常显示的属性:</p><pre class=" language-php"><code class=" language-php"><span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">makeHidden<span class="token punctuation">(</span></span><span class="token string">'attribute'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="appending-values-to-json"></a></p><h2><a href="#appending-values-to-json">添加参数到 JSON 中</a></h2><p>有时候,在转换模型到 数组 或 JSON 时,你希望添加一个在数据库中没有对应字段的属性。首先你需要为这个值定义一个 <a href="/docs/5.4/eloquent-mutators">访问器</a> :</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 为用户获取管理者的标记。
*
* @return bool
*/</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getIsAdminAttribute<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token this">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">attributes</span><span class="token punctuation">[</span><span class="token string">'admin'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'yes'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><p>访问器创建成功后,只需添加该属性到改模型的 <code class=" language-php">appends</code> 属性中。注意,属性名称通常遵循 「<a href="https://en.wikipedia.org/wiki/Snake_case">Snake Case</a>」 的命名方式,即是访问器的名称是基于 「<a href="https://en.wikipedia.org/wiki/Camel_case">Camel Case</a>」 的命名方式。</p><pre class=" language-php"><code class=" language-php"><span class="token delimiter"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span>
<span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/**
* 访问器被附加到模型数组的形式。
*
* @var array
*/</span>
<span class="token keyword">protected</span> <span class="token variable">$appends</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'is_admin'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><p>一旦属性被添加到 <code class=" language-php">appends</code> 清单,便会将模型中的数组和 JSON 这两种形式都包含进去。在 <code class=" language-php">appends</code> 数组中的属性也遵循模型中 <code class=" language-php">visible</code> 和 <code class=" language-php">hidden</code> 设置。</p><h2>译者署名</h2><table><thead><tr><th>用户名</th><th>头像</th><th>职能</th><th>签名</th></tr></thead><tbody><tr><td><a href="https://laravel-china.org/users/79">@skyverd</a></td><td><img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/79_1427370664.jpeg?imageView2/1/w/100/h/100"></td><td>翻译</td><td>全桟工程师,<a href="https://skyverd.com">时光博客</a></td></tr></tbody></table></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度