以`User`模型为例,模型定义如下:
```
<pre class="calibre18">
```
<?php
namespace app\index\model;
use think\<span class="hljs-operator">Model</span>;
<span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword"><span class="hljs-operator">extends</span></span> <span class="hljs-title">Model</span></span>{
}
```
```
可以输出模型实例对象为数组或者`JSON`。
- - [输出数组](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u8F93u51FAu6570u7EC4)
- [隐藏属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u9690u85CFu5C5Eu6027)
- [指定属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u6307u5B9Au5C5Eu6027)
- [追加属性](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#u8FFDu52A0u5C5Eu6027)
- [输出JSON](http://ihavenolimitations.xyz/thinkphp/thinkphp5_quickstart/147377#-json)
## 输出数组
可以使用`toArray`方法把当前的模型对象输出为数组。
修改`User`控制器的`read`操作方法如下:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据并输出数组</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>);
dump(<span class="hljs-regexp">$user</span>->toArray());
}
```
```
访问 `http://tp5.com/user/20` 页面输出结果为:
```
<pre class="calibre18">
```
<span class="hljs-keyword">array</span> (size=<span class="hljs-number">7</span>)
<span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>)
<span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>)
<span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>)
<span class="hljs-string">'status'</span> => string <span class="hljs-string">'待审核'</span> (length=<span class="hljs-number">9</span>)
<span class="hljs-string">'create_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>)
<span class="hljs-string">'update_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>)
```
```
可以看到,User模型的数据已经经过了读取器方法处理。
## 隐藏属性
如果输出的时候需要隐藏某些属性,可以使用:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据并输出数组</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>);
dump(<span class="hljs-regexp">$user</span>->hidden([<span class="hljs-string">'create_time'</span>,<span class="hljs-string">'update_time'</span>])->toArray());
}
```
```
再次访问 `http://tp5.com/user/20` 页面输出结果变成:
```
<pre class="calibre18">
```
<span class="hljs-keyword">array</span> (size=<span class="hljs-number">5</span>)
<span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>)
<span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>)
<span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>)
<span class="hljs-string">'status'</span> => int <span class="hljs-number">2</span>
```
```
## 指定属性
或者指定一些属性输出,则可以用:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据并输出数组</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>);
dump(<span class="hljs-regexp">$user</span>->visible([<span class="hljs-string">'id'</span>,<span class="hljs-string">'nickname'</span>,<span class="hljs-string">'email'</span>])->toArray());
}
```
```
再次访问 `http://tp5.com/user/20` 页面输出结果变成:
```
<pre class="calibre18">
```
<span class="hljs-keyword">array</span> (size=<span class="hljs-number">3</span>)
<span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>)
<span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>)
```
```
## 追加属性
如果读取器定义了一些非数据库字段的读取,例如:
```
<pre class="calibre18">
```
<span class="hljs-operator"><span class="hljs-number"><?php</span><span class="hljs-keyword">namespace</span> <span class="hljs-title">app</span>\<span class="hljs-title">index</span>\<span class="hljs-title">model</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">think</span>\<span class="hljs-title">Model</span>;
<span class="hljs-operator"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Model</span></span>{
<span class="hljs-comment">// status修改器</span><span class="hljs-keyword">protected</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUserStatusAttr</span><span class="hljs-number">(<span class="hljs-regexp">$value</span>)</span></span>{
<span class="hljs-regexp">$status</span> = [-<span class="hljs-number">1</span> => <span class="hljs-string">'删除'</span>, <span class="hljs-number">0</span> => <span class="hljs-string">'禁用'</span>, <span class="hljs-number">1</span> => <span class="hljs-string">'正常'</span>, <span class="hljs-number">2</span> => <span class="hljs-string">'待审核'</span>];
<span class="hljs-keyword">return</span> <span class="hljs-regexp">$status</span>[<span class="hljs-regexp">$value</span>];
}
}</span>
```
```
而我们如果需要输出`user_status`属性数据的话,可以使用`append`方法,用法如下:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据并输出数组</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>);
dump(<span class="hljs-regexp">$user</span>->append([<span class="hljs-string">'user_status'</span>])->toArray());
}
```
```
再次访问 `http://tp5.com/user/20` 页面输出结果变成:
```
<pre class="calibre18">
```
<span class="hljs-keyword">array</span> (size=<span class="hljs-number">8</span>)
<span class="hljs-string">'id'</span> => int <span class="hljs-number">20</span><span class="hljs-string">'nickname'</span> => string <span class="hljs-string">'张三'</span> (length=<span class="hljs-number">6</span>)
<span class="hljs-string">'email'</span> => string <span class="hljs-string">'zhanghsan@qq.com'</span> (length=<span class="hljs-number">16</span>)
<span class="hljs-string">'birthday'</span> => string <span class="hljs-string">'1988/01/15'</span> (length=<span class="hljs-number">10</span>)
<span class="hljs-string">'status'</span> => int <span class="hljs-number">2</span><span class="hljs-string">'create_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>)
<span class="hljs-string">'update_time'</span> => string <span class="hljs-string">'2016-05-02 16:40:57'</span> (length=<span class="hljs-number">19</span>)
<span class="hljs-string">'user_status'</span> => string <span class="hljs-string">'待审核'</span> (length=<span class="hljs-number">9</span>)
```
```
## 输出JSON
对于`API`开发而言,经常需要返回`JSON`格式的数据,修改`read`操作方法改成`JSON`输出:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据输出JSON</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-regexp">$user</span> = UserModel::get(<span class="hljs-regexp">$id</span>);
<span class="hljs-keyword">echo</span> <span class="hljs-regexp">$user</span>->toJson();
}
```
```
访问 `http://tp5.com/user/20` 页面输出结果为:
```
<pre class="calibre18">
```
{"<span class="hljs-regexp">id</span>":<span class="hljs-string"><span class="hljs-number">22</span></span>,"<span class="hljs-regexp">nickname</span>":<span class="hljs-string"><span class="hljs-operator">"张三"</span></span>,"<span class="hljs-regexp">email</span>":<span class="hljs-string"><span class="hljs-operator">"zhanghsan@qq.com"</span></span>,"<span class="hljs-regexp">birthday</span>":<span class="hljs-string"><span class="hljs-operator">"1988\/01\/15"</span></span>,"<span class="hljs-regexp">status</span>":<span class="hljs-string"><span class="hljs-number">2</span></span>,"<span class="hljs-regexp">create_time</span>":<span class="hljs-string"><span class="hljs-operator">"2016-05-02 16:40:57"</span></span>,"<span class="hljs-regexp">update_time</span>":<span class="hljs-string"><span class="hljs-operator">"2016-05-02 16:40:57"</span></span>}
```
```
或者采用更简单的方法输出`JSON`,下面的方式是等效的:
```
<pre class="calibre18">
```
<span class="hljs-comment">// 读取用户数据直接输出JSON</span><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">read</span><span class="hljs-number">(<span class="hljs-regexp">$id</span> = <span class="hljs-string">''</span>)</span></span>{
<span class="hljs-keyword">echo</span> UserModel::get(<span class="hljs-regexp">$id</span>);
}
```
```
> toJson输出方法仍然支持 `hidden`、`visible`和`append`方法。
- 脕茫隆垄脨貌脩脭
- 脕茫隆垄脨貌脩脭
- 脪禄隆垄禄霉麓隆
- 脪禄隆垄禄霉麓隆
- 露镁隆垄URL潞脥脗路脫脡
- 露镁隆垄URL潞脥脗路脫脡
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脠媒隆垄脟毛脟贸潞脥脧矛脫娄
- 脣脛隆垄脢媒戮脻驴芒
- 脣脛隆垄脢媒戮脻驴芒
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脦氓隆垄虏茅脩炉脫茂脩脭
- 脕霉隆垄脛拢脨脥潞脥鹿脴脕陋
- 拢篓1拢漏脛拢脨脥露篓脪氓
- 拢篓2拢漏禄霉麓隆虏脵脳梅
- 拢篓3拢漏露脕脠隆脝梅潞脥脨脼赂脛脝梅
- 拢篓4拢漏脌脿脨脥脳陋禄禄潞脥脳脭露炉脥锚鲁脡
- 拢篓5拢漏虏茅脩炉路露脦搂
- 拢篓6拢漏脢盲脠毛潞脥脩茅脰陇
- 拢篓7拢漏鹿脴脕陋
- 拢篓8拢漏脛拢脨脥脢盲鲁枚
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 脝脽隆垄脢脫脥录潞脥脛拢掳氓
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 掳脣隆垄碌梅脢脭潞脥脠脮脰戮
- 戮脜隆垄API驴陋路垄
- 戮脜隆垄API驴陋路垄
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐隆垄脙眉脕卯脨脨鹿陇戮脽
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐脪禄隆垄脌漏脮鹿
- 脢庐露镁隆垄脭脫脧卯
- Cookie
- Session
- 碌楼脭陋虏芒脢脭
- 脥录脧帽麓娄脌铆
- 脦脛录镁脡脧麓芦
- 脩茅脰陇脗毛
- 赂陆脗录
- A隆垄鲁拢录没脦脢脤芒录炉
- B隆垄3.2潞脥5.0脟酶卤冒
- C隆垄脰煤脢脰潞炉脢媒
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝
- 路卢脥芒脝陋拢潞脩搂脧掳ThinkPHP5碌脛脮媒脠路脳脣脢脝