zoukankan      html  css  js  c++  java
  • laravel5.2数据库基本操作

    laravel5.2数据库基本操作

    百牛信息技术bainiu.ltd整理发布于博客园

    use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes;
    use DB;
    class User extends Model {
    static function getResult(){ $res=null;
    /* //插入
    $user=array('username'=>'test','password'=>'123456','age'=>23); $res = DB::table('users')->insert($user);
    */
    /* //查询
    res = DB::table('users')->where('username','test')->first();//一维 $res = DB::table('users')->where('username','test')->get(); //二维
    $res = DB::select('SELECT * FROM users WHERE username="test"'); */
    /*
    //更新数据 $res =DB::update('UPDATE users SET age=25 WHERE username="test"');
    $res =DB::table('users')->where('username','test')->update(array('age' =>23)); */
    /*
    //删除数据 $res = DB::delete('DELETE FROM users WHERE username="joy"');
    $res = DB::table('users')->where('username','joy')->delete(); */
    /*
    //字段添加 $res = DB::statement('ALTER TABLE users ADD sex tinyint(1) not null default 0');
    */
    /*事务 DB::transaction(function(){
    $user=array(xxxx); DB::table('user')->insert($user);
    DB..... });
    */
    /* //结果查询器
    $res = DB::table('users')->where('username','test')->pluck('age');//去特定字段值 $res = DB::table('users')->lists('age');//特殊字段list值
    $res = DB::table('users')->select(array('id','username'))->get();多个特殊字段查询 $res = DB::table('users')->where('id','>',1)->get();
    $res = DB::table('users')->where('id',1)->get(); $res = DB::table('users')->whereId(1)->get();
    $res = DB::table('users')->where('id','<',3)->where('username','test')->get();
    $res = DB::table('users')->where('id','>',3)->orWhere('username','test')->get();
    $res = DB::table('users')->where('username','test')->where(function($query){
    $query->where('age','>',13)->orWhere('sex',0); })->get();//相当于username = 'test' and (age>13 or sex=0)
    $res = DB::table('users')->whereBetween('age',array(0,15))->get();
    $res = DB::table('users')->whereNotBetween('age',array(0,15))->get(); $res = DB::table('users')->whereIn('age',array(23,30))->get();
    $res = DB::table('users')->whereNot In('age',array(23,30))->get(); */

    //$res = DB::table('users')->groupBy('sex')->orderBy('id','DESC')->having('sex','=',0)->get(); //$res = DB::table('users')->limit(2,3)->get();
    //$res = DB::table('users')->skip(2)->take(1)->get();
    //$res = DB::table('posts')->join('users','posts.user_id','=','users.id')->get(); // $res = DB::table('posts')->join('users',function($query){
    // $query->on('posts.user_id','=','users.id');//->On(...); // })->get();
    /*
    //嵌套查询 $res = DB::table('users')->where(function($query){
    if(Input::get('status')){ $query->whereStatus(Input::get('status'));
    } if(Input::get('source')){
    $query->whereSource(Input::get('source')); }
    })->get(); */
    /*
    $res = DB::table('users')->whereExists(function($query){ $query->select('*')->from('posts')->whereRaw(DB::raw('users.id = posts.user_id'));
    })->get(); */
    $res = DB::table('users')->where('id','>','2')->count();
    $res = DB::table('posts')->where('id',6)->decrement('view_count',5);//减5 //$res=DB::table('posts')->truncate();//清表

    print_r($res);exit; }
    }

  • 相关阅读:
    Codeforces Round #380(div 2)
    Codeforces Round #378(div 2)
    Codeforces Round #379(div 2)
    CCPC2016合肥现场赛
    CCPC2016沈阳站
    HDU2222 Keywords Search__AC自动机
    poj2185Milking Grid
    POJ2961_kmp
    POJ 2406
    poj 2752Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/bainiu/p/7550571.html
Copyright © 2011-2022 走看看