tp5 model toArray 会将时间戳转为年月日
tp5模型的toArray方法不会自动将时间戳转换为年月日。该方法只是将模型对象转换为数组格式,包括模型对象的属性和值。如果想要将时间戳转换为年月日格式,可以在模型中定义一个访问器来处理时间戳的转换。
例如,在模型中定义一个访问器来处理时间戳的转换:
namespace app\model;
use think\Model;
class MyModel extends Model
{
// 定义时间戳字段
protected $autoWriteTimestamp = true;
// 定义一个访问器来转换时间戳为年月日格式
public function getCreateTimeAttr($value)
{
return date('Y-m-d', $value);
}
}
然后,在使用toArray方法获取模型数据时,会自动调用访问器来处理时间戳的转换:
$model = MyModel::find(1);
$data = $model->toArray();
此时,$data中的时间戳字段createTime会被转换为年月日格式
原文地址: https://www.cveoy.top/t/topic/iuOP 著作权归作者所有。请勿转载和采集!