摘要:<?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();//查詢構(gòu)造器方法查詢
$res = Books::all(function($query)
{
$query->where('state',1)->order('id','desc');//使用閉包查詢
});
var_dump($res);
}
//添加
public function Create()
{
$data=[
"title"=>'php精通到放棄',
"brief"=>'為什么要學(xué)php能?作為世界上最好的語言,可以讓你變成高薪白領(lǐng)。但是你需要努力學(xué)習(xí)',
"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);
}
}
批改老師:西門大官人批改時(shí)間:2019-05-05 10:27:33
老師總結(jié):所謂軟刪除,實(shí)際上就是update記錄的刪除狀態(tài)