abstrait:<?phpnamespace app\index\model;use think\Model;use think\model\concern\SoftDelete; //Trait 方法集class Tushu extends Model{ use SoftDelete; //設(shè)置數(shù)據(jù)庫(kù)字段ID名稱(chēng) protected $pk = 'id'; // 設(shè)置當(dāng)前模型對(duì)應(yīng)的完整數(shù)據(jù)
<?php
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete; //Trait 方法集
class Tushu extends Model
{
use SoftDelete;
//設(shè)置數(shù)據(jù)庫(kù)字段ID名稱(chēng)
protected $pk = 'id';
// 設(shè)置當(dāng)前模型對(duì)應(yīng)的完整數(shù)據(jù)表名稱(chēng)
protected $table = 'tushu';
// 設(shè)置當(dāng)前模型的數(shù)據(jù)庫(kù)連接
protected $connection = 'tp51';
//軟刪除字段默認(rèn)值
protected $defaultSoftDelete = 0;
//軟刪除字段
protected $deletetime = 'delete_time';
}
————————————————————————————————————————————
<?php
namespace app\index\controller;
use app\index\model\Tushu as ModelTushu;
use think\Controller;
class Tushu extends Controller
{
public function chaxun()
{
dump(ModelTushu::get(1));
//$a = ModelTushu::where('id',1)->select();
//dump($a);
//外部傳值方式
$a=$this->request->param('jiage')?:100;
$res = ModelTushu::all(function($query) use($a){
$query->where('jiage','>',$a);
});
dump($res);
}
public function tianjia()
{
$data=[
'name'=>'紅樓夢(mèng)',
'jiage'=>'250'
];
ModelTushu::create($data);
}
public function gengxin()
{
$jiage=50;// \think\Db::raw('jiage+200') 如果jiage加一個(gè)$jiage變量,怎么寫(xiě)
$tiaozhengjiage=500;
ModelTushu::update(['jiage'=>\think\Db::raw('jiage+200')],function($query) use($tiaozhengjiage){
$query->where('jiage','<',$tiaozhengjiage);
});
}
public function shanchu()
{
ModelTushu::destroy(1);
ModelTushu::destroy([1,2,3]);
//閉包
ModelTushu::destroy(function($query){
$query->where('id','>',10);
});
}
public function ruanshanchu()
{
ModelTushu::destroy(1);
//查看包括軟刪除數(shù)據(jù)
//dump(ModelTushu::withTrashed()->all());
//只看軟刪除數(shù)據(jù)
//dump(ModelTushu::onlyTrashed()->all());
}
}
Professeur correcteur:西門(mén)大官人Temps de correction:2019-02-17 11:48:57
Résumé du professeur:如果jiage加一個(gè)$jiage變量。直接寫(xiě)變量就行了。注意用雙引號(hào)而不是單引號(hào)。雙引號(hào)中的php變量會(huì)解析運(yùn)行,單引號(hào)中的不會(huì)