class Banner
{
public function getBanner($id)
{
$validate = new IDMustBePositiveInt();
$validate->goCheck();
$banner = BannerModel::getBannerById($id);
//返回的因为是受保护的类型,不能直接访问其成员,要先转成数组
$data = $banner->toArray();
unset($data[‘dalete_time‘]); //删除delete_time字段
if (!$banner) {
throw new BannerMissException();
}
return $data;
}
}
class Banner
{
public function getBanner($id)
{
$validate = new IDMustBePositiveInt();
$validate->goCheck();
$banner = BannerModel::getBannerById($id);
if (!$banner) {
throw new MissException([
‘msg‘ => ‘请求banner不存在‘,
‘errorCode‘ => 40000
]);
}
//传进一个要隐藏的字段数组
$banner->hidden([‘delete_time‘, ‘update_time‘])
//传进一个只显示的字段数组
// $banner->visible([‘id‘])
return $banner;
}
}
查看返回结果
application\api\model\Banner.php
class Banner extends Model
{
protected $hidden = [‘id‘]; // visible方法也可用
}
查看返回结果
原文:https://www.cnblogs.com/Qyhg/p/14756722.html