Blogger Information
Blog 52
fans 0
comment 3
visits 42606
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel框架的数据库相关操作
王小飞
Original
857 people have browsed it

简介

Laravel 的数据库查询构造器为创建和运行数据库查询提供了一个方便的接口。它可用于执行应用程序中大部分数据库操作,且可在所有支持的数据库系统上运行。

Laravel 的查询构造器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。因此没有必要清理作为绑定传递的字符串。

Laravel查询构造器中文文档

https://learnku.com/docs/laravel/5.7/queries/2289

查询多条记录

  1. public function list(){
  2. echo '<pre>';
  3. $res = DB::table('jizhang')->get();
  4. print_r($res);
  5. }

分类查询 只查询某个字段

  1. public function fllist(){
  2. echo '<pre>';
  3. $res = DB::table('jizhang')->select('yonghuid as yid','jine')->get();
  4. print_r($res);
  5. }

筛选查询 只查询用户id=28的

  1. public function idlist(){
  2. echo '<pre>';
  3. $res = DB::table('jizhang')->select('yonghuid','jine')->where('yonghuid','=',28)->get();
  4. print_r($res);
  5. }

like 查询 关键词查询 可以加多个where进行更精细的筛选 orWhere

  1. public function likes(){
  2. $res = DB::table('jizhang')->where('beizhu','like','%顺丰%')->get()->all();
  3. echo '<pre>';
  4. print_r($res);
  5. }

where in 查询

  1. public function wherein(){
  2. $res = DB::table('jizhang')->whereIn('id',[1,18,22])->get()->all();
  3. echo '<pre>';
  4. print_r($res);
  5. }

连表查询

  1. public function joins(){
  2. $res = DB::table('jizhang')->join('users','users.id','=','jizhang.yonghuid')->get()->all();
  3. echo '<pre>';
  4. print_r($res);
  5. }

计算平均值

  1. public function pvs(){
  2. $res=DB::table('jizhang')->avg('pv');
  3. echo $res;
  4. // $avg = 0;
  5. // foreach ($res as $key => $value){
  6. // $avg += $value->pv;
  7. // }
  8. // $avg = $avg/count($res);
  9. // echo '<pre>';
  10. // print_r($avg);
  11. }

增加数据

  1. public function insert2(){
  2. $item = array('jine'=>50,'zhanghu'=>"现金",'chengyuan'=>"小飞",'beizhu'=>5555,'shijian'=>1589108925,'yonghuid'=>28,'pv'=>50);
  3. $item2 = array('jine'=>50,'zhanghu'=>"现金",'chengyuan'=>"小飞",'beizhu'=>5555,'shijian'=>1589108925,'yonghuid'=>28,'pv'=>50);
  4. $data[] = $item;
  5. $data[] = $item2;
  6. $res = DB::table('jizhang')->insert($data);
  7. var_dump($res);
  8. }

  • 上图是刚新增的数据

增加数据并返回主键

  1. public function insert3(){
  2. $item = array('jine'=>50,'zhanghu'=>"现金",'chengyuan'=>"小飞",'beizhu'=>5555,'shijian'=>1589108925,'yonghuid'=>28,'pv'=>50);
  3. $res = DB::table('jizhang')->insertGetId($item);
  4. var_dump($res);
  5. }

  • 上图是返回的主键id

修改数据 wherein可以修改多条

  1. public function update2(){
  2. $res = DB::table('jizhang')->where('id',15)->update(array('jine'=>10500));
  3. var_dump($res);
  4. }
  5. `

  • 上图是修改后的效果 金额改为10500

删除数据whereIn可以删除多条

  1. public function shanchu(){
  2. $res = DB::table('jizhang')->where('id',16)->delete();
  3. var_dump($res);
  4. }

  • 上图是删除了id为16的记录效果

总结:本节课深入学习了数据库的各种操作,第一次明白了前台的各种筛选条件原来是用数据库的查询语句筛选出来的。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:数据库主要是curd,但只会curd也不行
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post