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步: 初 ...
随机推荐
- Docsify+腾讯云对象存储 COS,一键搭建云上静态博客
最近一直在想如何利用 COS 简化静态博客的搭建过程.搜了很多的静态博客搭建过程,发现大部分的静态博客都要通过编译才能生成静态页面.功夫不负有心人,终于让我找到了一个超简洁博客的搭建方法. 效果预览 ...
- IdentityServer4系列 | 快速搭建简易项目
一 .前言 从上一篇关于 常见术语说明中,主要是对IdentityServer4的说明,以及其中涉及常见的术语的表述说明,包括对身份认证服务器.用户.客户端.资源以及各个令牌等进行对比区别说明. 而在 ...
- JZOJ8月15日提高组反思——2020年暑假终结篇
JZOJ8月15日提高组反思--2020年暑假终结篇 T1 仙人掌最短路 抱歉我只会最短路 仙人掌是啥? 听说是缩点+\(LCA\) 最短路30 T2 直接暴力计算 正解\(DP\) \(amazin ...
- https中间人攻击
攻击过程: 服务器向客户端发送公钥. 攻击者截获公钥,保留在自己手上. 然后攻击者自己生成一个[伪造的]公钥,发给客户端. 客户端收到伪造的公钥后,生成加密hash值发给服务器. 攻击者获得加密has ...
- BJWC2011 禁忌
题目链接 题解 多模式匹配首先建 AC 自动机,看到 \(len \le 10^9\) 想到矩阵乘法优化. 朴素 DP 关于分割的最大值,可以贪心,只要走到一个能匹配串的点立刻返回根继续匹配就行,一定 ...
- linux的Umask 为022 和027 都是什么意思?
用全部权限777去减这个数值 一.022表示默认创建新文件权限为755 也就是 rxwr-xr-x(所有者全部权限,属组读写,其它人读写) 二.027表示默认创建新文件权限为750 也就是rxwr- ...
- 廖雪峰官网学习js 数组
indexOf( ) 某字符的位置 slice 相当于string 的substring 切片 a = ['a','b',1,2,3] (5) ["a", "b&q ...
- Editor.md解决跨域上传的问题
Editor.md解决跨域上传的问题 编辑 editormd\plugins\image-dialog\image-dialog.js 替换以下代码片段 if (settings.crossDomai ...
- 六、TestNG传递参数1
TestNG可以通过testng.xml和Data Providers向测试方法传递参数 利用testNG.xml传递参数 1-创建一个TestNG测试类 其中 parameters = {" ...
- Service Cloud 零基础(二)Knowledge浅谈
本篇参考:https://trailhead.salesforce.com/content/learn/projects/set-up-salesforce-knowledge https://tra ...