批改状态:合格
老师批语:改了用户名了。。。 方法名推荐使用驼峰命名
// 查询数据多条数据// 调用get()返回一个对象集合,all()将对象集合改为数组集合public function list(){$res=DB::table('atitle')->get();echo '<pre>';// print_r($res->item);print_r($res->all());print_r($res);}

// 查询数据并按条件显示public function list(){// where('字段名','符号','条件')$res=DB::table('atitle')->select('cate_id as cid','title')->where('cate_id',8)->get();echo '<pre>';// print_r($res->item);print_r($res->all());// print_r($res);}

//like查询public function likes(){$res= DB::table('atitle')->where('title','like','%6%')->get()->all();//$res = DB::table('atitle')->where('cate_id',5)->orwhere('cate_id',8)->tosql();// tpsql()返回 select * from `atitle` where `cate_id` = ? or `cate_id` = ?echo '<pre>';print_r($res);}

// 连表查询public function joins(){$res= DB::table('atitle')->join('user','user.id','=','atitle.uid')->select('user.id','atitle.uid','user.username','cate_id','title')->get()->all();echo '<pre>';print_r($res);}

public function pvs(){// 计算平均值$res = DB::table('atitle')->avg('pv');$res =(int)$res;print($res);// 计算平均值$res = DB::table('atitle')->get();$avg = 0;foreach($res as $key => $value){$avg += $value->pv;}$avg = $avg/count($res);$avg =(int)$avg;echo '<pre>';print_r($avg);}

// 增加数据public function insert2(){// 插入一条记录// $res= DB::table('atitle')->insert(array('id'=>6,'uid'=>2,'cate_id'=>6,'title'=>'sssss','pv'=>200));// 插入多条记录$item =array('uid'=>2,'cate_id'=>6,'title'=>'sssss','pv'=>200);$item2 =array('uid'=>2,'cate_id'=>6,'title'=>'ss5555sss','pv'=>200);$data[]=$item;$data[]=$item2;$res = DB::table('atitle')->insert($data);var_dump($res);}

insertGetId()
// 增加数据并返回主键public function insert3(){$item =array('uid'=>2,'cate_id'=>6,'title'=>'返回ID测试','pv'=>200);$res = DB::table('atitle')->insertGetId($item);var_dump($res);}

// 修改数据public function update2(){// $res = DB::table('atitle')->where('id',10)->update(array('title'=>'更改测试'));// 修改数据多条数据$res = DB::table('atitle')->whereIn('id',[5,8,10])->update(array('title'=>'更改测试'));var_dump($res);}

// 删除public function delete2(){// 删除一条数据// $res =DB::table('atitle')->where('id',8)->delete();// 删除多条数据$res =DB::table('atitle')->whereIn('id',[9,10])->delete();var_dump($res);}

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Atitle extends Model{// 不指定表明时,类名就是表名// 指定表名protected $table= 'user';}
// 模型public function mymodels(atitle $atitle){// $res= $atitle->get()->all();// toarry()将对象转为数组$res= $atitle->get()->toArray();echo '<pre>';print_r($res);}

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号