Blogger Information
Blog 53
fans 3
comment 0
visits 46619
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel查询构造器方法演示练习
emagic
Original
935 people have browsed it

9月16日作业

一、使用查询构造器get value pluck insert update delete方法至少各写一个示例

1.查询构造器get()方法,输出的是Collect对象

  1. // 查询全部 get()方法,输出的是Collect对象
  2. public function lists()
  3. {
  4. $res = DB::table('xpcms_admin')->where('id','>',15)->get();
  5. //table('表名')
  6. echo "<pre>";
  7. print_r($res);
  8. }

2.查询构造器toArray()方法,将Collect对象输出为array

  1. // toArray()方法,将Collect对象输出为array
  2. public function toarray()
  3. {
  4. $res = DB::table('xpcms_admin')->where('id','>',1)->get(['id','username','real_name','mobile']);
  5. //table('表名')
  6. $data = $res->toArray();
  7. echo "<pre>";
  8. print_r($data);
  9. }

3.查询构造器value()方法查询某一列的值

  1. // 查询某一列的值,单列,所以不用传数组
  2. public function val(){
  3. $res1 = DB::table('xpcms_admin')->where('id',23)->first('real_name');
  4. $res2 = DB::table('xpcms_admin')->where('id',23)->value('real_name');
  5. echo "<pre>";
  6. print_r($res1);
  7. echo "<hr>";
  8. print_r($res2);
  9. }

4.查询构造器pluck()方法

  1. public function pluck(){
  2. $res = DB::table('xpcms_admin')->limit(5)->get('username');
  3. $res2 = DB::table('xpcms_admin')->limit(5)->pluck('username');
  4. $res3 = DB::table('xpcms_admin')->limit(5)->pluck('username','real_name');
  5. echo "<pre>";
  6. print_r($res);
  7. echo "<hr>";
  8. print_r($res2);
  9. echo "<hr>";
  10. print_r($res3);
  11. }
  • 注意,pluck('参数1查询的字段名','参数2字段名用于显示自定义的key)第二个参数可以自定义输出的key

5.链式插入insert()

  1. // 链式插入
  2. public function insert()
  3. {
  4. $data = [['username'=>'testuser','password'=>'123456789','real_name'=>'测试账号1'],['username'=>'testuser22','password'=>'9888884455','real_name'=>'测试账号2']];
  5. $res = DB::table('xpcms_admin')->insert($data);
  6. print_r( $res);
  7. }

6. 链式insertGetId(),返回单条插入成功的记录id

  1. public function insertGetId()
  2. {
  3. $data = ['username'=>'testuser3','password'=>'123456789','real_name'=>'测试账号3'];
  4. $res = DB::table('xpcms_admin')->insertGetId($data);
  5. print_r( $res);
  6. }
  • 注意 insertGetId只能插入单条,并返回单条插入成功的记录id。不支持多条。

7.链式调用update()

  1. public function update()
  2. {
  3. $res = DB::table('xpcms_admin')->where('group_id',3)->update(['group_id'=>4]);
  4. print_r($res);
  5. }
  • 注意 一定要加入where 条件控制,反之数据库错误覆盖

8.链式调用delete()

  1. public function delete()
  2. {
  3. $res = DB::table('xpcms_admin')->where('id',28)->delete();
  4. print_r($res);
  5. }
  • 注意 一定要加入where 条件控制,反之数据库错误删错数据

二、聚集方法:count sum max min avg方法至少各写一个示例

  1. / 聚合查询
  2. // 最大值max()
  3. public function juhemax()
  4. {
  5. $res = DB::table('xpcms_admin')->max('id');
  6. print_r($res);
  7. }
  8. // 最小值min()
  9. public function juhemin()
  10. {
  11. $res = DB::table('xpcms_admin')->min('id');
  12. print_r($res);
  13. }
  14. // 平均值 average()
  15. public function average()
  16. {
  17. //avg('字段名') 对哪个字段进行计算
  18. $res = DB::table('xpcms_admin')->avg('id');
  19. print_r($res);
  20. }
  21. // 平均值 average()
  22. public function sum()
  23. {
  24. //sum('字段名') 对哪个字段进行求和,只能传一个字段,不能多个
  25. $res = DB::table('xpcms_admin')->sum('id');
  26. print_r($res);
  27. }
  28. // 记录总数 count()
  29. public function count()
  30. {
  31. $res = DB::table('xpcms_admin')->count();
  32. print_r($res);
  33. }
  34. //链式查询器 as 重命名字段名
  35. public function rename()
  36. {
  37. $res = DB::table('xpcms_admin')->select('id','username as name','real_name as nickname' )->get();
  38. echo '<pre>';
  39. print_r($res);
  40. }
  41. // 使用场景:接口中隐藏字段真实名称,防止暴露
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:查询构造器是后面ORM的基础与底层实现逻辑
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