// 取回数据表的第一条数据
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类的更多相关文章

  1. nodejs操作mongodb数据库封装DB类

    这个DB类也算是我经历了3个实际项目应用的,现分享出来,有需要的请借鉴批评. 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关 ...

  2. nodejs mongodb 数据库封装DB类 -转

    使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mongoose 这个DB类的数据库配置是 ...

  3. tp5数据库操作 Db类

    一.链接数据库 1.配置文件定义  application\database.php 注意:数据表前缀更改,在文件的prefix选项 2.类定义 二.数据库的基本使用 namespace app\de ...

  4. Discuz!数据库操作DB类和C::t类介绍

    类定义文件 DB类: 文件\source\class\class_core.php class DB extends discuz_database {} discuz_database类定义 文件\ ...

  5. 封装类似thinkphp连贯操作数据库的Db类(简单版)。

    <?php header("Content-Type:text/html;charset=utf-8"); /** *php操作mysql的工具类 */ class Db{ ...

  6. PHP——laravel之DB类->查询

    DB类之查询: 满足条件的全部获取:DB::table("表名")->where("name",">","1" ...

  7. Laravel5.2中Eloquent与DB类的区别是什么?

    要了解这些先看看关于数据库组件的那些事儿(就是 Eloquent ORM) 数据库组件大概分了三层: 数据库连接层 查询构造层 应用层 来看一下每一层有哪些东西,分别对应文档的哪一部分: 数据库连接层 ...

  8. 封装自己的DB类(PHP)

    封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现.这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便! 1.封装一个DB类.一个类文件 ...

  9. 封装DB类

    封装DB类     一般一个类单独书写在一个Php文件中,为了见名知意,会对文件名有一个规范:类名.class.php 第1步:     创建DB类 第2 步:     属性设计 第3步:     初 ...

随机推荐

  1. 六. Vue CLI详解

    1. Vue CLI理解 1.1 什么是Vue CLI 如果你只是简单写几个Vue的Demo程序, 那么你不需要Vue CLI,如果你在开发大型项目那么你需要它, 并且必然需要使用Vue CLI. 使 ...

  2. day7(vue发送短信)

    1.vue发送短信逻辑 前端函数如下,js方法代码无需更改,前端代码逻辑在components\common\lab_header.vue 只需要修改components\axios_api\http ...

  3. 《MySQL慢查询优化》之SQL语句及索引优化

    1.慢查询优化方式 服务器硬件升级优化 Mysql服务器软件优化 数据库表结构优化 SQL语句及索引优化 本文重点关注于SQL语句及索引优化,关于其他优化方式以及索引原理等,请关注本人<MySQ ...

  4. Python中sort、sorted的cmp参数废弃之后使用cmp_to_key实现类似功能

    Python2.1以前的排序比较方法只提供一个cmp比较函数参数,没有__lt__等6个富比较方法, Python 2.1引入了富比较方法,Python3.4之后作废了cmp参数.相应地从Python ...

  5. Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?

    在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...

  6. PyQt学习问题:Model/View中中EditKeyPressed常量平台编辑键(the platform edit key )是什么?

    老猿在学习PyQt的Model/View设计时,发现是否允许对视图中的数据项进行编辑的函数setEditTriggers的参数QAbstractItemView.EditTriggers是几个常量的组 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中toolBar的allowedAreas属性

    1.概述 allowedAreas属性指定工具栏允许移动的范围,其类型为枚举类Qt.ToolBarAreas,有如下取值: 以上取值可以同or操作组合使用. 2.访问方法 缺省值为Qt.AllTool ...

  8. Thinkphp V5.X 远程代码执行漏洞 - POC(搬运)

    文章来源:lsh4ck's Blog 原文链接: https://www.77169.com/html/237165.html Thinkphp 5.0.22   http://192.168.1.1 ...

  9. 团队作业part5--测试与发布(Alpha版本)

    测试报告 1.测试与解决bug 1)测试人员测试出的bug 游戏失败后方块还能下落 分数设计不太合理 存在行数不可消除的情况 2)开发人员解决bug 2.场景测试 适用群体:打发时间的学生.工作压力大 ...

  10. Srcum冲刺_Day05

    一.团队展示: 1.项目:light_note备忘录 2.队名:删库跑路队 3.团队成员 队员(不分先后) 项目角色 黄敦鸿 后端工程师.测试 黄华 后端工程师.测试 黄骏鹏 后端工程师.测试 黄源钦 ...