Blogger Information
Blog 87
fans 1
comment 0
visits 58852
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel基础3
阿杰
Original
493 people have browsed it

学习查询构造器

  1. 链式查询单条数据方法 - first()

    1. public function item()
    2. {
    3. // 链式调用
    4. $res = DB::table('admin')->where(['ispasswd'=>0])->first(['id','username']);
    5. echo '<pre>';
    6. print_r($res);
    7. }

  2. 查询多条记录方法 - get()

    1. // 查询多条记录
    2. public function lists()
    3. {
    4. $res = DB::table('admin')->where('ispasswd',1)->get();
    5. $data = $res->toArray();
    6. echo '<pre>';
    7. print_r($data);
    8. }


    给 get() 方法里传参

    1. // 查询多条记录
    2. public function lists()
    3. {
    4. $res = DB::table('admin')->where('ispasswd',1)->get(['id','real_name']);
    5. $data = $res->toArray();
    6. echo '<pre>';
    7. print_r($data);
    8. }

  3. 查询构造器 - value()

    1. // 查询某一列的当前值
    2. public function val(){
    3. $res = DB::table('admin')->where(['ispasswd'=>1])->first(['real_name']);
    4. $res2 = DB::table('admin')->where('ispasswd',1)->value('real_name');
    5. echo '<pre>';
    6. print_r($res);
    7. print_r($res2);
    8. }

    first()与value()的区别

  4. 查询构造器 - pluck()

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


    pluck()与get()的区别

    1. public function pluck(){
    2. $res = DB::table('admin')->pluck('real_name');
    3. $res2 = DB::table('admin')->get('real_name');
    4. echo '<pre>';
    5. print_r($res);
    6. print_r($res2);
    7. }


    pluck()与value()类似亲兄弟

    1. public function pluck(){
    2. $res = DB::table('admin')->pluck('real_name');
    3. $res2 = DB::table('admin')->value('real_name');
    4. echo '<pre>';
    5. print_r($res);
    6. print_r($res2);
    7. }


    pluck()第二个参数可以替换索引下标

    1. public function pluck(){
    2. $res = DB::table('admin')->pluck('real_name');
    3. $res2 = DB::table('admin')->value('real_name');
    4. $res3 = DB::table('admin')->pluck('real_name','id');
    5. echo '<pre>';
    6. print_r($res);
    7. print_r($res2);
    8. print_r($res3);
    9. }

  5. 查询构造器 - insert()

    1. // 新增记录
    2. public function insert(){
    3. $res = DB::table('admin')->insert(['username'=>'xiaohu','password'=>'dfdfdfdf','ispasswd'=>1,'gid'=>3,'real_name'=>'胡哥']);
    4. var_dump($res);
    5. }



    插入数据并获取数据表主键id

    1. public function insert(){
    2. // $res = DB::table('admin')->insert(['username'=>'xiaohu','password'=>'dfdfdfdf','ispasswd'=>1,'gid'=>3,'real_name'=>'胡哥']);
    3. // var_dump($res);
    4. $data = ['username'=>'xiaohu2','password'=>'dfdfdfdfsss','ispasswd'=>1,'gid'=>2,'real_name'=>'胡哥2'];
    5. $res2 = DB::table('admin')->insertGetId($data);
    6. var_dump($res2);
    7. }


  6. 查询构造器 - update()

    1. // 更新记录
    2. public function update(){
    3. $res = DB::table('admin')->where('ispasswd',0)->update(['ispasswd'=>2]);
    4. var_dump($res);
    5. }



  7. 查询构造器 - delete()

    1. // 删除记录
    2. public function del(){
    3. $res = DB::table('admin')->where('id',6)->delete();
    4. var_dump($res);
    5. }



  8. 聚合查询 max、min、avg、sum 、count

    1. // 聚合查询
    2. public function juhe(){
    3. // 最大值
    4. $res = DB::table('admin')->max('gid');
    5. // 最小值
    6. $res2 = DB::table('admin')->min('gid');
    7. // 平均值
    8. $res3 = DB::table('admin')->avg('gid');
    9. // 求和
    10. $res4 = DB::table('admin')->sum('gid');
    11. // 记录总数
    12. $res5 = DB::table('admin')->where('gid',1)->count();
    13. var_dump($res);
    14. echo '<hr>';
    15. var_dump($res2);
    16. echo '<hr>';
    17. var_dump($res3);
    18. echo '<hr>';
    19. var_dump($res4);
    20. echo '<hr>';
    21. var_dump($res5);
    22. }

  9. where条件查询

    1. // where
    2. public function where(){
    3. $res = DB::table('admin')->where('gid','>',1)->get();
    4. echo '<pre>';
    5. print_r($res);
    6. }

总结

  • get()方法相比first()来说可以查询多条数据,可以通过参数获取相对应字段的集合,对象集合可以通过toArray()来获取数组
  • value()是获取到数据表中某个字段的值,返回的也只是一条数据,pluck()和value()相似,但它接收的是数组集合,pluck()第二个参数可以替换索引下标
  • insert()、update()、delete()作为链式查询中的增删改,返回的都是影响的行数结果
  • count()、sum()、max()、min()、avg()作为聚合查询,返回的是查询结果的类型和具体值
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