## 定義不存在的字段
在獲取數據輸出時,需要用到表不存在的字段,就需要用到模型中的獲取器
~~~
<?php
namespace app\index\model;
use think\Model;
class User extends Model{
//定義不存在的字段
protected $append = ['status_text'];
//獲取數據時自動添加字段
public function getStatusTextAttr($value,$data)
{
$status = [-1=>'刪除',0=>'禁用',1=>'正常',2=>'待審核'];
return $status[$data['status']];
}
}
~~~
>[success] $data為當前的所有數據數組
調用模型方法
~~~
$user = User:find(1);
輸出:
[
'id'=>1,
'status' => 1
'status_text' => '正常'
]
~~~