Laravel框架基础操作

2017-11-20 guoweidong php开发


        数据库查询
        $test = DB::select('select * from 表名 where id = ?',[3]);
        dd($test);
        //数据库插入
        $bool = DB::insert('insert into 表名 (name) values (?)',['sean']);
        var_dump($bool);
         数据库修改
        $update = DB::update('update 表名 set name = ? where id = ? ',['gwd',3]);
        var_dump($update);
         数据库删除

        $delete = DB::delete('delete from 表名 where id = ?',[3]);

        查询构造器Add
        $bool = DB::table('test')->insert(
                [ 'name' => 'GG']
         );

        $bool = DB::table('test')->insertGetid(
                ['name' => 'HH']
        );

        $bool = DB::table('test')->insert(
              [
                  ['name' => 'KK'],

                  ['name' => 'JJ']
              ]
        );

        查询构造器 Update

        $num = DB::table('test')
           ->where('id',12)
           ->update(['name'=>'DD']);
        $num = DB::table('test')->increment('name');
        $num = DB::table('test')->increment('name',3);
        $num = DB::table('test')->decrement('name',3);
        $num = DB::table('test')
            ->where('id',12)
            ->decrement('name',3);

        查询构造器 Delete

         $num = DB::table('test')
            ->where('id',12)
            ->delete();
        $num = DB::table('test')
            ->where('id','>=',10)
            ->delete();
        var_dump($num);
        DB::table('test')->truncate();

        查询构造器 Select

        获取表的全部数据
        $students = DB::table('test')->get();
        第一条数据
        $students = DB::table('test')
            ->orderBy('id' , 'desc')
            ->first();
        where条件查找
        $students = DB::table('test')
            ->where('id' ,'=', '1')
            ->get();
        where多条件查找
        $students = DB::table('test')
            ->whereRaw('id = ? and name = ?' ,[1 , 'KK'])
            ->get('id');
        返回某字段
        $students = DB::table('test')
            ->pluck('name');
        返回某字段 + 下标
        $students = DB::table('test')
            ->lists('name','id');
        //返回指定几个字段
        $students = DB::table('test')
            ->select('id' , 'name')
            ->get();
        //俩俩查询
        DB::table('test')->chunk(2,function($student){
            var_dump($student);
        });
        聚合函数

        $num = DB::table('test')->count();
        $num = DB::table('test')->max('id');
        $num = DB::table('test')->min('id');
        $num = DB::table('test')->avg('id');



网站备案号:京ICP备11043289号-1 北京市公安局网络备案 海1101084571
版权所有 北京育灵童科技发展有限公司 Copyright © 2002-2024 www.elight.cn, All Rights Reserved