abstract:<?phpnamespace app\index\model;use think\Model;use think\model\concern\SoftDelete; //Trait 方法集class Tushu extends Model{ use SoftDelete; //设置数据库字段ID名称 protected $pk = 'id'; // 设置当前模型对应的完整数据
<?php
namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete; //Trait 方法集
class Tushu extends Model
{
use SoftDelete;
//设置数据库字段ID名称
protected $pk = 'id';
// 设置当前模型对应的完整数据表名称
protected $table = 'tushu';
// 设置当前模型的数据库连接
protected $connection = 'tp51';
//软删除字段默认值
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'=>'红楼梦',
'jiage'=>'250'
];
ModelTushu::create($data);
}
public function gengxin()
{
$jiage=50;// \think\Db::raw('jiage+200') 如果jiage加一个$jiage变量,怎么写
$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);
//查看包括软删除数据
//dump(ModelTushu::withTrashed()->all());
//只看软删除数据
//dump(ModelTushu::onlyTrashed()->all());
}
}
Correcting teacher:西门大官人Correction time:2019-02-17 11:48:57
Teacher's summary:如果jiage加一个$jiage变量。直接写变量就行了。注意用双引号而不是单引号。双引号中的php变量会解析运行,单引号中的不会