laravel 数据库之DB类
// 取回数据表的第一条数据
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();
laravel 数据库之DB类的更多相关文章
- nodejs操作mongodb数据库封装DB类
这个DB类也算是我经历了3个实际项目应用的,现分享出来,有需要的请借鉴批评. 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关 ...
- nodejs mongodb 数据库封装DB类 -转
使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mongoose 这个DB类的数据库配置是 ...
- tp5数据库操作 Db类
一.链接数据库 1.配置文件定义 application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...
- Discuz!数据库操作DB类和C::t类介绍
类定义文件 DB类: 文件\source\class\class_core.php class DB extends discuz_database {} discuz_database类定义 文件\ ...
- 封装类似thinkphp连贯操作数据库的Db类(简单版)。
<?php header("Content-Type:text/html;charset=utf-8"); /** *php操作mysql的工具类 */ class Db{ ...
- PHP——laravel之DB类->查询
DB类之查询: 满足条件的全部获取:DB::table("表名")->where("name",">","1" ...
- Laravel5.2中Eloquent与DB类的区别是什么?
要了解这些先看看关于数据库组件的那些事儿(就是 Eloquent ORM) 数据库组件大概分了三层: 数据库连接层 查询构造层 应用层 来看一下每一层有哪些东西,分别对应文档的哪一部分: 数据库连接层 ...
- 封装自己的DB类(PHP)
封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现.这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便! 1.封装一个DB类.一个类文件 ...
- 封装DB类
封装DB类 一般一个类单独书写在一个Php文件中,为了见名知意,会对文件名有一个规范:类名.class.php 第1步: 创建DB类 第2 步: 属性设计 第3步: 初 ...
随机推荐
- modelviewset与apiview
modelviewset(认证,权限,限流,序列化,分页,过滤,排序) 1.DRF初始化 1.认证(让用户登录) 2.权限(根据不同用户角色,可以操作不同的表) 3.限流(限制接口访问速度) 4.序列 ...
- python虚拟环境相关设置备忘
sudo pip install virtualenv #安装虚拟环境 sudo pip install virtualenvwrapper #安装虚拟环境管理工具nano ~/.bashrc #修 ...
- 老猿学5G:融合计费基于QoS流计费QBC的触发器Triggers
☞ ░ 前往老猿Python博文目录 ░ 一.引言 SMF中的功能体CTF在用户上网时达到一定条件就会向CHF上报流量,而CTF什么时候触发流量上报是由CTF中的触发器来控制的.在<老猿学5G: ...
- 第8.33节 Python中__getattr__以及__getattr__与__ getattribute__的关系深入剖析
一. 引言 前面几节分别介绍了Python中属性操作捕获的三剑客:__ getattribute__方法.__setattr__方法.__delattr__方法,为什么__ getattribute_ ...
- PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButtons标准按钮
在Qt Designer中,可以在界面中使用QDialogButtonBox来配置一组按钮进行操作,Qt中为QDialogButtonBox定义了一组常用的标准按钮,可以在Designer中直接在St ...
- python基本案例实现
案例一:test.txt文件中与输入的用户进行认证,超过3次用户被锁定,且把用户加入锁定的lock.txt文件中. # 需求点: # 1.输入用户名.密码 # 2.认证成功后显示欢迎信息 # 3.输错 ...
- 性能测试基础——(MEN)
关于内存在一块其实我并不是很想拿出来说,一般情况下内存这一块都是可优化的,可以通过硬件资源或者调整一些系统或者应用系统的参数配置来进行优化. 很多同僚问到了"内存泄漏"和" ...
- js onreadystatechange 和 onload的区别
IE的script 元素只支持onreadystatechange事件,不支持onload事件. FF的script 元素不支持onreadystatechange事件,只支持onload事件. 如果 ...
- 「IOI2017」接线 的另类做法
看到这题,我的第一反应是:这就是一个费用流模型?用模拟费用流的方法? 这应该是可以的,但是我忘记了怎么模拟费用流了IOI不可能考模拟费用流.于是我就想了另外一个方法. 首先我们考虑模拟费用流的模型如下 ...
- Android Studio/IDEA插件
1.android parcelable code generator 2.android code generator3.gson format4.android postfix completio ...