laravel 数据库之DB类

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

laravel 数据库之DB类

风_雨   2020-12-29 我要评论
// 取回数据表的第一条数据 DB::table('table')->where('key', 'value')->first(); DB::table('table')->first(); // 从单行中取出单列数据 DB::table('users')->where('key', 'value')->pluck('id'); DB::table('name')->pluck('id'); // 取多行数据的「列数据」数组 DB::table('roles')->lists('title', 'name'); // 指定一个选择字句 DB::table('users')->select('key', 'value')->get(); DB::table('users')->distinct()->get(); DB::table('users')->select('name as user_name')->get(); // 添加一个选择字句到一个已存在的查询语句中 $query = DB::table('users')->select('name'); $users = $query->addSelect('age')->get(); // 取得数据表的所有行 DB::table('name')->get(); // 取数据表的部分数据 DB::table('users')->chunk(100, function($users) { foreach ($users as $user){ // 逻辑编辑 } }); // 使用 Where 运算符 DB::table('users')->where('votes', '>', 100)->get(); DB::table('users') ->where('votes', '>', 100) ->orWhere('name', 'John') ->get(); DB::table('users') ->whereBetween('votes', [1, 100])->get(); DB::table('users') ->whereNotBetween('votes', [1, 100])->get(); DB::table('users') ->whereIn('id', [1, 2, 3])->get(); DB::table('users') ->whereNotIn('id', [1, 2, 3])->get(); DB::table('users') ->whereNull('updated_at')->get(); DB::table('name')->whereNotNull('column')->get(); // 动态的 Where 字句 DB::table('users') ->whereId(1)->first(); DB::table('users') ->whereIdAndEmail(2, '12345678@qq.com') ->first(); DB::table('users') ->whereNameOrAge('Jane', 22) ->first(); // Order By, Group By, 和 Having DB::table('users') ->orderBy('name', 'desc') ->groupBy('count') ->having('count', '>', 100) ->get(); DB::table('name')->orderBy('column')->get(); DB::table('name')->orderBy('column','desc')->get(); DB::table('name')->having('count', '>', 100)->get(); // 偏移 & 限制 $users = DB::table('users')->skip(10)->take(5)->get(); // 基本的 Join 声明语句 DB::table('users') ->join('contacts', 'users.id', '=', 'contacts.user_id') ->join('orders', 'users.id', '=', 'orders.user_id') ->select('users.id', 'contacts.phone', 'orders.price') ->get(); // Left Join 声明语句 DB::table('users') ->leftJoin('posts', 'users.id', '=', 'posts.user_id') ->get(); // select * from users where name = 'John' or (votes > 100 and title <> 'Admin') DB::table('users') ->where('name', '=', 'John') ->orWhere(function($query) { $query->where('votes', '>', 100) ->where('title', '<>', 'Admin'); }) ->get(); //聚合 DB::table('users')->count(); DB::table('orders')->max('price'); DB::table('orders')->min('price'); DB::table('orders')->avg('price'); DB::table('users')->sum('votes'); DB::table('name')->remember(5)->get(); DB::table('name')->remember(5, 'cache-key-name')->get(); DB::table('name')->cacheTags('my-key')->remember(5)->get(); DB::table('name')->cacheTags(array('my-first-key','my-second-key'))->remember(5)->get(); DB::table('users') ->select(DB::raw('count(*) as user_count, status')) ->where('status', '<>', 1) ->groupBy('status') ->get(); // 返回行 DB::select('select * from users where id = ?', array('value')); DB::insert('insert into foo set bar=2'); DB::update('update foo set bar=2'); DB::delete('delete from bar'); // 返回 void DB::statement('update foo set bar=2'); // 在声明语句中加入原始的表达式 DB::table('name')->select(DB::raw('count(*) as count, column2'))->get(); Inserts / Updates / Deletes / Unions / Pessimistic Locking // 插入 $id = DB::table('users')->insertGetId( ['email' => '12345678@qq.com', 'votes' => 0] ); DB::table('users')->insert([ ['email' => '12345678@qq.com', 'votes' => 0], ['email' => '12345678@qq.com', 'votes' => 0] ]); // 更新 DB::table('users') ->where('id', 1) ->update(['votes' => 1]); DB::table('users')->increment('votes'); DB::table('users')->increment('votes', 5); DB::table('users')->decrement('votes'); DB::table('users')->decrement('votes', 5); DB::table('users')->increment('votes', 1, ['name' => 'John']); // 删除 DB::table('users')->where('votes', '<', 100)->delete(); DB::table('users')->delete(); DB::table('users')->truncate(); // 运行数据库查询语句 $results = DB::select('select * from users where id = ?', [1]); $results = DB::select('select * from users where id = :id', ['id' => 1]); // 运行普通语句 DB::statement('drop table users'); // 监听查询事件 DB::listen(function($sql, $bindings, $time){ code_here; }); // 数据库事务处理 ① 第一种 DB::transaction(function(){ DB::table('users')->update(['votes' => 1]); DB::table('posts')->delete(); }); ②第二种 DB::beginTransaction(); DB::rollback(); DB::commit();

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们