多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
一、实现原生sql查询 $list = DB::select($sql); //结果集是json格式 二、实现原生数据库连接 $dsn = 'mysql:host=127.0.0.1;dbname=mysql'; $user = env('DB_USERNAME',''); $pass = env('DB_PASSWORD',''); $db = new \PDO($dsn, $user, $pass); //连接 $db->exec("SET names utf8"); //设置uft8,exec可以执行所需的sql语句 三、检索表中的一行(原生) $user = DB::table('users')->where('name', 'John')->first(); var_dump($user->name); 四、检索表中的所有行(原生) $users = DB::table('users')->get(); foreach ($users as $user) { var_dump($user->name); } 五、检索单个列的行(原生) $name = DB::table('users')->where('name', 'John')->pluck('name'); 六、 指定一个Select子句(查询所需字段) $users = DB::table('users')->select('name', 'email')->get();   $users = DB::table('users')->distinct()->get();   $users = DB::table('users')->select('name as user_name')->get(); 七、Select子句添加到一个现有的查询 $query = DB::table('users')->select('name'); $users = $query->addSelect('age')->get(); 八、where $users = DB::table('users')->where('votes', '>', 100)->get(); 九、OR $users = DB::table('users')->orWhere('votes', '>', 100)->get(); 十、Where Between $users = DB::table('users')->whereBetween('votes', array(1, 100))->get(); 十一、Where Not Between $users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get(); 十二、Where In $users = DB::table('users')->whereIn('id', array(1, 2, 3))->get(); $users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get(); 十三、Order By, Group By, And Having $users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get(); 十四、Offset & Limit $users = DB::table('users')->skip(10)->take(5)->get(); 十五、左连接语句 DB::table('users')    ->leftJoin('posts', 'users.id', '=', 'posts.user_id')    ->get(); DB::table('users') ->join('contacts', function($join) { $join->on('users.id', '=', 'contacts.user_id')->orOn(...); }) ->get(); DB::table('users') ->join('contacts', function($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); }) ->get(); 十六、高级的where子句 DB::table('users') ->where('name', '=', 'John') ->orWhere(function($query) { $query->where('votes', '>', 100) ->where('title', '<>', 'Admin'); }) ->get(); 十七、count&max&min&avg&sum $users = DB::table('users')->count(); $price = DB::table('orders')->max('price'); $price = DB::table('orders')->min('price'); $price = DB::table('orders')->avg('price'); $total = DB::table('users')->sum('votes'); 十八、递增或递减一个列的值 DB::table('users')->increment('votes', 5); DB::table('users')->decrement('votes'); 十九、指定额外的列更新: DB::table('users')->increment('votes', 1, array('name' => 'John')); 二十、插入数据 DB::table('users')->insert( array('email' => 'john@example.com', 'votes' => 0) ); 二十一、插入一个记录并获得当前插入的id(需是只增id) $id = DB::table('users')->insertGetId( array('email' => 'john@example.com', 'votes' => 0) ); 二十二、更新操作 DB::table('users') ->where('id', 1) ->update(array('votes' => 1)); GoodsRobot::where('good_id', $v['goodId'])->update(array('order_time' => $time));//更详下单时间 二十三、删除操作 DB::table('users')->where('votes', '<', 100)->delete(); 二十四、删除表中的所有记录 DB::table('users')->delete(); 二十四、删除一个表 DB::table('users')->truncate(); 二十五、查询构建器还提供了一种快速的方法来“联盟”两个查询: $first = DB::table('users')->whereNull('first_name');   $users =DB::table('users')->whereNull('last_name')->union($first)->get(); 二十六、SELECT语句“共享锁”,你可以使用sharedLock方法查询: DB::table('users')->where('votes', '>',100)->sharedLock()->get(); 二十七、更新“锁”在一个SELECT语句,您可以使用lockForUpdate方法查询: DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get(); 二十八、缓存查询 $users = DB::table('users')->remember(10)->get(); 二十九、原样输出 {!! $buyerShareInstruction !!} 三十、获取指定字段 ActivitySeller::where('seller_id', $sellerId)->lists('activity_id');//返回的是一维数组 三十一、高级用法 $list->where(function($sql) use($activity_ids){ $sql->where(function($query){ $query->where('is_system', 1)->where('use_seller', 0)->where('seller_id', 0); }) ->orWhere(function($query) use($activity_ids){ $query->where('is_system', 1)->where('use_seller', 1)->whereIn('id', $activity_ids); }); }); 三十二、获取sql DB::connection()->enableQueryLog(); 中间是sql执行体(5.0之后怎么用的哈,5.0之前不是这样用哈) dd(DB::getQueryLog());