模型curd软删除操作

Original 2019-05-03 13:48:51 348
abstract:<?php  namespace app\index\controller; use think\Controller; use app\index\model\Books; class Booksl extends Controller { //查询 public function Query() { //$res = Books::where('state&#

<?php 

namespace app\index\controller;


use think\Controller;

use app\index\model\Books;


class Booksl extends Controller

{

//查询

public function Query()

{

//$res = Books::where('state',1)->select();//查询构造器方法查询


$res = Books::all(function($query)

{

$query->where('state',1)->order('id','desc');//使用闭包查询

});


var_dump($res);

}

//添加

public function Create()

{

$data=[

"title"=>'php精通到放弃',

"brief"=>'为什么要学php能?作为世界上最好的语言,可以让你变成高薪白领。但是你需要努力学习',

"author"=>'大哥大',

"state"=>0

];

Books::create($data);

}

//修改

public function Update()

{

$id = 1;

$author = '嘻嘻洗';

$state = 1;

Books::update(['author'=>"$author",'state'=>"$state"],function($query) use($id){

$query->where('id',"$id");

});

}

//删除

public function Delete()

{

$id = 5;

Books::destroy(function($query)use($id){

$query->where('id',"$id");

});

}

//软删除

public function SoftDele()

{

$id = 7;

Books::destroy(function($query)use($id){

$query->where('id',"$id");

});

$res = Books::withTrashed()->select();

dump($res);

// Books::destroy(1,true);

// // $res = Books::withTrashed()->select();

// // dump($res);

}

}


Correcting teacher:西门大官人Correction time:2019-05-05 10:27:33
Teacher's summary:所谓软删除,实际上就是update记录的删除状态

Release Notes

Popular Entries