toArray()); } public function model2(){ $modelObj = new Demo(); $result = $modelObj->where("category_id",3) ->limit(2) ->select(); // dump($result); foreach ($result as $resultvar){ // dump($resultvar->title); dump($resultvar['title']); dump($resultvar->status_text); } } /** * 查询数据 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function index(){ // //第一种 通过门面模式的方式获取 // $result = Db::table("mall_demo")->where("id",2)->find(); // dump($result); // //通过容器的方式来获取 // $result = app("db")->table("mall_demo")->where("id",2)->find(); // dump($result); //获取一条 $result = Db::table("mall_demo") ->order("id","desc") ->find(); dump($result); //获取获取多条 $result = Db::table("mall_demo") ->order("id","desc") ->limit(2,2) //可以做分页的逻辑 --- 这个需要计算开始值 ->select(); dump($result); //获取获取多条 -- 分页方式 $result = Db::table("mall_demo") ->order("id","desc") ->page(1,2) //可以做分页的逻辑 --- 这个不需要计算开始值 ->select(); dump($result); //获取获取多条 -- 范围值 $result = Db::table("mall_demo") ->where("id",">",2) ->select(); dump($result); //获取获取多条 -- 多条件 $result = Db::table("mall_demo") ->where("id",">",2) ->where("category_id","=",3) ->select(); dump($result); //获取获取多条 -- 多条件 $result = Db::table("mall_demo") ->where([ ["id",">",2], ["category_id","=",3], ["id","in","4,5,6"] ]) ->select(); dump($result); } /** * 打印sql * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function abc(){ //第一种 --- 打印sql $result = Db::table("mall_demo")->where("id",2)->fetchSql()->find(); dump($result); //第二种 --- 打印sql $result = Db::table("mall_demo")->where("id",2)->find(); echo Db::getLastSql(); exit; } /** * 新增逻辑 */ public function demo(){ $data = [ "title" => "深刻的反思就", "category_id" => 4, ]; $result = Db::table("mall_demo")->insert($data); echo Db::getLastSql(); dump($result); } /** * 真正删除逻辑 */ public function demodel(){ $result = Db::table("mall_demo")->where("id",1)->delete(1); echo Db::getLastSql(); dump($result); } /** * 真正更新逻辑 */ public function demoupdate(){ $result = Db::table("mall_demo") ->where("id",2) ->update([ "title" => "真是的哎", ]); echo Db::getLastSql(); dump($result); } }