laravel5.5中查询构造器的使用

//查询构造器新增数据:
    public function query1()
    {

        /*        $bool=DB::table('student')->insert(
                    ['name'=>'小李','age'=>12]
                );
                var_dump($bool);*/

        //获取插入的id
        /*        $ids = DB::table('student')->insertGetId(
                    ['name' => 'php', 'age' => 22]
                );
                var_dump($ids);*/

        $bool = DB::table('student')->insert([
                ['name' => '小李001', 'age' => 12],
                ['name' => '小李002', 'age' => 121]
            ]
        );
        var_dump($bool);

    }

    //查询构造器更新数据:
    public function query2()
    {
        /*      $num=DB::table('student')
                  ->where('id',1004)
                  ->update(['age'=>101]);
              var_dump($num);*/

        //自增1
        /* $num=DB::table('student')->increment('age');*/
//自增3
        /* $num=DB::table('student')->increment('age',3);*/

        //自减1
        /*$num=DB::table('student')->decrement('age');*/

        //自减3
        /*  $num=DB::table('student')->increment('age',3);
          var_dump($num);*/

        //带条件自减
        /*        $num=DB::table('student')
                    ->where('id',1004)
                    ->decrement('age',3);
                var_dump($num);*/

        $num = DB::table('student')
            ->where('id', 1004)
            ->increment('age', 3, ['name' => '小王']);
        var_dump($num);
    }

    //使用查询构造器删除数据:
    public function query3()
    {
        /*        $num=DB::table('student')
                    ->where('id','>=',1008)
                    ->delete();
                var_dump($num);*/

        //清空数据表
        DB::table('student')->truncate();
    }

    //使用查询构造器查询数据:
    public function query4()
    {
        /*        $num=DB::table('student')->insert([
                    ['id'=>1001,'name'=>'name1','age'=>18],
                    ['id'=>1002,'name'=>'name2','age'=>19],
                    ['id'=>1003,'name'=>'name3','age'=>28],
                    ['id'=>1004,'name'=>'name4','age'=>38],
                    ['id'=>1005,'name'=>'name5','age'=>78]
                ]);
                var_dump($num);*/

        //get(); 获取表中所有数据
        /*        $students=DB::table('student')->get();
                dd($students);*/

        //first() 获取第一条数据:
        /*$students=DB::table('student')
            ->orderBy('id','desc')
            ->first();
        dd($students);*/

        //where查询数据:
        /*$students=DB::table('student')
            ->where('id','>=',1002)
            ->get();
        dd($students);*/

        //加多个条件进行查询
        /*$students=DB::table('student')
            ->whereRaw('id>=? and age>?',[1003,38])
            ->get();
        dd($students);*/

        //pluck()
        /*$names=DB::table('student')
            ->pluck('name');
        dd($names);*/

        //lists 5.3之后已经弃用
        /*        $names=DB::table('student')
                    ->lists('name','id');
                dd($names);*/

        //select() 指定查找:
        /*$students=DB::table('student')
            ->select('id','name','age')->get();
        dd($students);*/

        //chunk:
        echo '<pre>';
        $students = DB::table('student')
            ->orderBy('id', 'asc')
            ->chunk(2, function ($students) {
                var_dump($students);
                return false; //查询出一组数据出来:
            });
    }

    //查询构造器中的聚合函数:
    public function query5()
    {
        //count() 统计数据数量
        /*$num=$num=DB::table('student')->count();
        var_dump($num);*/

        /*$max=$num=DB::table('student')->max('age');
        var_dump($max);*/

        /*$min=$num=DB::table('student')->max('age');
        var_dump($min);*/

        /*$avg=$num=DB::table('student')->avg('age');
        var_dump($avg);*/

        $sum = $num = DB::table('student')->sum('age');
        var_dump($sum);
    }

  

posted @ 2017-11-13 14:42  生如逆旅,一苇以航  阅读(363)  评论(0编辑  收藏  举报