1. 模型时间戳
一般不开启database配置文件 里的'aotu_timestamp =true'
而可以在model里单独开启,可以如下开启时间戳的功能
public class User{
protected $autoWriteTimestamp = true;
}
注意
protected $autoWriteTimestamp = true;
开启后,默认会对数据表 中 create_time 和 update_time 字段自动完成
如果数据表中的字段名 不是create_time 和 update_time 则需要 申明他们的属性
也可以关闭 单独某字段的自动完成
public class User{
protected $autoWriteTimestamp = true;
//新增数据的时间戳字段,默认为create_time
protected $createTime = 'create_tm';
//更新数据的时间戳字段,默认为update_time
//protected $updateTime = 'update_tm';
//关闭updateTime
protected $updateTime = false;
}
2. 软删除
在数据表中建 (默认) delete_time 字段,默认为null, 记录被删除的时间戳
<?php
use traits\model\SoftDelete
public class User{
use SoftDelete; //开启了软删除
protected $autoWriteTimestamp = true;
protected $createTime = 'create_tm';
}
若 想更改默认的delete_time 字段
在model中使用:
protected $deleteTime = 'delete_tm';
3. 获取(包含被软删除的数据) 使用 withTrashed(true)
例如
$res = User::withTrashed(true)->find(2);
4. 仅获取被删除的数据
$res = User::onlyTrashed()->select();//返回数组
5. 恢复被软删除的数据
直接 update delete_time 字段
=============================
6. 如果想物理删除数据
使用
$res = User::destroy(2,true);
或者
$user = User::get(2);
$res = $user->delete(true);