模型输出
[TOC]
对象-->数组
对象可以通过数组的形式打印出来
$user = User::get(1);
//dump($user->toArray());
//dump($user->hidden(['name','id'])->toArray());
dump($user->visible(['name','id'])->toArray());
还可以配合读取器来输出:
$user = User::get(1);
$user->status=2;
dump($user->append(['status'])->toArray());
status可以是不存在的字段
读取器:
public function getStatusAttr($value){
$status = [-1 => '删除',0 => '禁用',1 => '正常',2 => '待审核'];
return $status[$value];
}
对象->JSON
$user = User::get(1);
echo $user->toJson();
//或者直接
echo $user;
网友评论