多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 从数据表中取得所有的数据列 ~~~ ~~~ $users = DB::table('users')->get(); foreach ($users as $user) { var_dump($user->name); } ~~~ ~~~ ### 从数据表中分块查找数据列 ~~~ ~~~ DB::table('users')->chunk(100, function($users) { foreach ($users as $user) { // } }); ~~~ ~~~ 通过在 闭包 中返回 false 来停止处理接下来的数据列: ~~~ ~~~ DB::table('users')->chunk(100, function($users) { // return false; }); ~~~ ~~~ ### 从数据表中取得单一数据列 ~~~ ~~~ $user = DB::table('users')->where('name', 'John')->first(); var_dump($user->name); ~~~ ~~~ ### 从数据表中取得单一数据列的单一字段 ~~~ ~~~ $name = DB::table('users')->where('name', 'John')->pluck('name'); ~~~ ~~~ ### 取得单一字段值的列表 ~~~ ~~~ $roles = DB::table('roles')->lists('title'); ~~~ ~~~ 这个方法将会返回数据表 role 的 title 字段值的数组。你也可以通过下面的方法,为返回的数组指定自定义键值。 ~~~ ~~~ $roles = DB::table('roles')->lists('title', 'name'); ~~~ ~~~ ### 指定查询子句 (Select Clause) ~~~ ~~~ $users = DB::table('users')->select('name', 'email')->get(); $users = DB::table('users')->distinct()->get(); $users = DB::table('users')->select('name as user_name')->get(); ~~~ ~~~ ### 增加查询子句到现有的查询中 ~~~ ~~~ $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') ->where('votes', '>', 100) ->orWhere('name', 'John') ->get(); ~~~ ~~~ ### 使用 Where Between ~~~ ~~~ $users = DB::table('users') ->whereBetween('votes', [1, 100])->get(); ~~~ ~~~ ### 使用 Where Not Between ~~~ ~~~ $users = DB::table('users') ->whereNotBetween('votes', [1, 100])->get(); ~~~ ~~~ ### 使用 Where In 与数组 ~~~ ~~~ $users = DB::table('users') ->whereIn('id', [1, 2, 3])->get(); $users = DB::table('users') ->whereNotIn('id', [1, 2, 3])->get(); ~~~ ~~~ ### 使用 Where Null 找有未配置的值的数据 ~~~ ~~~ $users = DB::table('users') ->whereNull('updated_at')->get(); ~~~ ~~~ ### Dynamic Where Clauses You may even use "dynamic" where statements to fluently build where statements using magic methods: ~~~ ~~~ $admin = DB::table('users')->whereId(1)->first(); $john = DB::table('users') ->whereIdAndEmail(2, 'john@doe.com') ->first(); $jane = DB::table('users') ->whereNameOrAge('Jane', 22) ->first(); ~~~ ~~~ ### 排序(Order By)、分群(Group By) 及 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(); ~~~ ~~~