Blogger Information
Blog 63
fans 8
comment 8
visits 50066
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP大牛成长之路:laravel查询构造器方法与聚合函数
周Sir-BLOG
Original
1153 people have browsed it

1. laravel查询构造器方法

1.0.1 获取第一条数据: first()

  1. //默认查询全部字段
  2. $users = DB::table('users')->first();
  3. //只返回username和email字段(数组表示)
  4. $users = DB::table('users')->first(['username','email']);

1.0.2 获取全部结果/转为数组: get() toArray()

  1. //默认查询全部字段-对象
  2. $users = DB::table('users')->get();
  3. //只返回username和email字段
  4. $users = DB::table('users')->get(['username','email']);
  5. //转为数组
  6. $users = DB::table('users')->get()->toArray();

1.0.3 获取第一条数据的(某个字段)字段值: value()

  1. $users = DB::table('users')->value('email');

1.0.4 获取单列值的集合: pluck(值) pluck(值,键)

  1. //键为索引数组
  2. $users = DB::table('users')->pluck('username');
  3. //指定email为键名
  4. $users = DB::table('users')->pluck('username', 'email');

1.0.5 通过 id 获取指定一条数据: find()

  1. $users = DB::table('users')->find(20);

1.0.6 设置查询条件: where

  1. //默认为等于
  2. $users = DB::table('users')->where('id', 19)->get();
  3. //操作符:>、 <、 >=、 <=、 <>、 like 等
  4. $users = DB::table('users')->where('age', '>=', 25)->get();
  5. $users = DB::table('users')->where('username', 'like', '%小%')->get();
  6. //多条件可以用数组
  7. $users = DB::table('users')->where([
  8. 'age' => 25,
  9. 'gender' => '男'
  10. ])->get();
  11. //如果条件非等于
  12. $users = DB::table('users')->where([
  13. ['age', '>=', 25],
  14. ['gender', '=', '男']
  15. ])->get();

1.0.7 返回指定记录数: limit()

  1. $users = DB::table('users')->limit(5)->get();

1.0.8 新增记录: insert()

  1. //新增一条记录
  2. DB::table('users')->insert([
  3. 'username' => '李白',
  4. 'password' => '123456',
  5. 'email' => 'libai@php.cn',
  6. ]);
  7. //新增多条记录
  8. DB::table('users')->insert([
  9. [...],
  10. [...]
  11. ]);

1.0.9 新增记录(返回新增后ID): insertGetId()

  1. //新增一条记录
  2. DB::table('users')->insertGetId([
  3. 'username' => '李白',
  4. 'password' => '123456',
  5. 'email' => 'libai@php.cn',
  6. ]);

1.1.0 更新修改一条数据: update()

  1. //更新修改一条数据
  2. DB::table('users')->where('id', 2202)->update([
  3. 'username' => '果冻',
  4. 'email' => 'guodong@php.cn'
  5. ]);

1.1.1 存在修改,不存在就新增: updateOrInsert()

  1. //参数 1:修改的条件
  2. //参数 2:修改的内容(新增的内容)
  3. DB::table('users')->updateOrInsert(
  4. ['id'=>2202],
  5. ['username'=>'洪海', 'password'=>'123456']
  6. );

1.1.2 删除改一条数据: delete()

  1. //默认索引
  2. DB::table('users')->delete(2202);
  3. //指定ID
  4. DB::table('users')->where('id', 2202)->delete();

使用 delete()删除数据, 一定要加上 where 条件, 否则数据会被清空

1.1.3 设置显示的字段/设置字段别名: select()

  1. //只显示username和email字段
  2. $users = DB::table('users')->select('username', 'email')->get();
  3. //设置username别名为name
  4. $users = DB::table('users')->select('username as name', 'email')->get();

2 聚合查询

  1. //max()
  2. return DB::table('users')->max('age');
  3. //min()
  4. return DB::table('users')->min('age');
  5. //avg()
  6. return DB::table('users')->avg('age');
  7. //sum()
  8. return DB::table('users')->sum('age');
  9. //count()
  10. return DB::table('users')->count();

count(*)函数不指定字段和指定字段查询效率没有区别,因为它只统计总的记录数。

总结

  • 对课堂内容结合手册将查询构造器方法与聚合函数进行了练习
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:常用的方法很多的, 有不少功能是重复的, 只不过是语法糖罢了
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