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’])方法追加字段