thinkphp追加输出数据库没有的不存在的字段
2022-07-28 14:14:58
137
{{single.collect_count}}

thinkphp追加数据库没有的字段

有时需要在输出字段里添加一个数据库中不存在的字段,这时可以使用模型的append方法实现

代码示例

public function getCurrentTimeAttr($value,$data){return time();}public function list($size = 10, $page = 1, $where = null){$list = $this->field('lo.*,o.order_no,o.price,o.status order_status,o.create_time deadline_time,l.image,l.name')->alias('lo')->join('order o', 'o.id = lo.order_id')->join('lecture l', 'l.id = lo.lecture_id')->withAttr('deadline_time', function ($value, $data) {return $value + 15 * 60;})->append(['current_time'])->where($where)->order(['lo.id' => 'desc'])->paginate(['list_rows' => $size,'page' => $page,]);return ['data' => $list, 'code' => 0, 'msg' => '成功'];}

需要追加当前时间戳current_time,先定义一个current_time的获取器getCurrentTimeAttr,然后使用append([‘current_time’])方法追加字段

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}