版本新增功能
5.0.10增加removeRelation方法去除所有的关联属性
5.0.5hiddenvisibleappend方法支持关联属性
5.0.4增加appendRelationAttr方法追加关联模型的属性

可以调用模型的toJson方法进行JSON序列化

$user = User::get(1);echo $user->toJson();

可以设置无需输出的字段,例如:

$user = User::get(1);echo $user->hidden(['create_time','update_time'])->toJson();

或者追加其它的字段:

$user = User::get(1);echo $user->append(['status_text'])->toJson();

设置允许输出的属性:

$user = User::get(1);echo $user->visible(['id','name','email'])->toJson();

模型对象可以直接被JSON序列化,例如:

echo json_encode(User::get(1));

输出结果类似于:

{"id":"1","name":"","title":"","status":"1","update_time":"1430409600","score":"90.5"}

或者也可以直接echo 一个模型对象,例如:

echo User::get(1);

输出的结果和上面是一样的。

追加关联模型的属性(V5.0.4+)

V5.0.4+版本开始,支持追加一对一关联模型的属性到当前模型,例如:

$user = User::find(1);echo $user->appendRelationAttr('profile',['email','nickname'])->toJson();

profile是关联定义方法名,emailnicknameProfile模型的属性。


 小秋同学  模型  2021-03-25 07:58:47    收藏:  赞(526)
学习吧_一个不错的学习网站
本文地址:https://www.qiuhai.com/doc/1668.html 复制链接 如果还有疑问,欢迎加入我们的QQ交流群:887790 加入Q群 易优最新程序下载: 点击下载
QQ在线咨询