【模型時間戳】
應用配置文件【不過一般不這樣用】
// 自動寫入時間戳字段
'auto_timestamp' => true,
【控制器塊】
namespace app\index\controller;
use think\Controller;
use app\index\model\App;
class Index extends Controller
{
public function index()
{
$res = App::create([
'name'=>'kail',
'is_encryption'=>1,
]);
dump($res);
}
}
【Model塊】
class App extends Model
{
use softDelete;//【數據庫字段為detele_time】
//默認字段是 create_time update_time
protected $autoWriteTimestamp = true;
//如果字段不是默認定義的字段[create_time update_time],要定義
// protected $createTime = 'create_at'; //【自定義的數據庫字段】
// protected $updateTime = 'update_at'; //【自定義的數據庫字段】
}
【軟刪除】
【控制器塊】
public function index()
{
//$res = App::destroy(20);
//$res = App::get(20); //軟刪除后獲取不到數據,為NULL
//要想獲取到包含軟刪除數據
//$res = App::withTrashed(true)->find(20);
//dump($res->getData());//獲取原始的數據
//想要獲取軟刪除的全部數據
// $res = App::onlyTrashed()->select();
// foreach($res as $val){
// dump($val->getData());
// }
//開啟軟刪除后想要真正的刪除
//$res = App::destroy(20,true);//【方式一】
$app = App::get(21);
$res = $app->delete(true);//【方式二】
dump($res);
}
【Model塊】
namespace app\index\model;
use think\Model;
use traits\model\SoftDelete;//【使用軟刪除】
class App extends Model
{
use SoftDelete;//【數據庫字段為detele_time】
//默認字段是 create_time update_time
protected $autoWriteTimestamp = true;
//如果字段不是默認定義的字段[create_time update_time],要定義
// protected $createTime = 'create_at'; //【自定義的數據庫字段】
// protected $updateTime = 'update_at'; //【自定義的數據庫字段】
}