// 取回数据表的第一条数据
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微博回调接口

    1.vue微博回调空页面 注:微博回调空页面为: http://127.0.0.1:8888/oauth/callback/ 1.1 页面路径 components\oauth.vue <tem ...

  2. 浅谈Abp vNext的模块化设计

    abp的模块化给我留下深刻的印象,模块化不是什么新概念,大家都习以为常,但是为什么要模块化,模块化的意义或者说目的是什么?也许我们思考得并不深入.难得的是abp不仅完美的阐述了模块化概念,而且把模块化 ...

  3. 修改MongDB的数据类型

    语法: db.集合.find({"列":{$type:2}}).forEach(function(x){ x.列=parseFloat(x.列);db.order.save(x) ...

  4. PyQt+moviepy音视频剪辑实战文章目录

    ☞ ░ 前往老猿Python博文目录 ░ 本专栏为moviepy音视频剪辑合成相关内容介绍的免费专栏,对应的收费专栏为<moviepy音视频开发专栏>. 一.moviepy基础能力系统介绍 ...

  5. react路由初探(2)

    对着官网的例子反正是没有搞出来,所以搜了一大堆,最终搞出来了,记录一下 import React from 'react'; // 首先我们需要导入一些组件... (这个是中文网示例,按这个做,报一大 ...

  6. 【NOI2018】你的名字(SAM & 线段树合并)

    Description Hint Solution 不妨先讨论一下无区间限制的做法. 首先"子串"可以理解为"前缀的后缀",因此我们定义一个 \(\lim(i) ...

  7. egg的基本使用

    一.脚手架(可以快速生成项目) 1.新建一个项目文件夹,使用如下命令: 2.npm  init  egg  --type=simple 3.npm i 它会根据package.json里记录的所需包进 ...

  8. Java集合源码分析(七)——TreeMap

    简介 TreeMap 是一个有序的key-value集合,它的内部是通过红黑树实现的. TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合. TreeMap ...

  9. Android Studio中SVN的使用

    1.忽略文件 1)这种方式,每次新建一个项目都要添加,并不是全局的. .idea文件夹 .gradle文件夹 所有的build文件夹 所有的.iml文件 local.properties文件 2)使用 ...

  10. undo表空间丢失、损坏

    1.模拟误删undo表空间 rm -rf undotbs01.dbf 2.解决步骤 SQL> shutdown immediateORA-01116: error in opening data ...