// 多个字段 $data = TestModel::selectRaw("name,status")->first(); // 复杂查询 $data = TestModel::selectRaw("CASE name WHEN 1 THEN 1 ELSE -1 END name")->first();
$data = TestModel::select("name")->where("status",1)->first(); // 或者 $data = TestModel::select("name")->where("status","=",1)->first(); // in 查询 $data = TestModel::select("name")->whereIn("status",[1,-1])->first(); // between // select name from `test` where `status` = ? and `status` between ? and ? $data = TestModel::select("name")->where("name","hello")->whereBetween("status",[1,-1])->first(); // between // select name from `test` where `status` = ? or `status` between ? and ? $data = TestModel::select("name")->where("name","hello")->whereBetween("status",[1,-1],"or")->first(); // between // select name from `test` where `status` = ? or `status` not between ? and ? $data = TestModel::select("name")->where("name","hello")->whereBetween("status",[1,-1],"or",true)->first(); // or $data = TestModel::select("name")->orWhere("status","=",1)->first();
// 这个方法返回一个二维数组、数组元素是一个stdClass对象。查询不到数据返回空数组。 DB::connection("mysql")->select("select * from user where status=? limit 1",[1]); // 或者,返回与上面的一致 $data = (new TestModel())->getConnection()->select("select * from user where status=? limit 1",[1]);