laravel 控制器类DB类操作
例子:TrGo表(trgo_chip):

laravel框架建立:TrGoModel
<?php
namespace TrChaos\Model; class TrGoModel extends Model
{
protected $table = 'trgo_chip';
protected $fillable = [
'id','item_id','sku_id','item_num','chip_code','created_at','updated_at','deleted_at'
];
} ?>
从数据表中取得所有的列:
<?php
$users = DB::table('user')->get();
foreach($users as $user){
var_dump($user->name);
} $users->toArray(); // 转换成数组 ?>
从数据表中取得单一数据列:
<?php
DB::table('users')->where('name', 'John')->first();
从数据表中取得单一数据列的单一字段:
<?php
$name = DB::table('users')->where('name','John')->pluck('name');
换做Laravel中的写法,先定义好Model
// 芯片记录
$chip_codes = TrGoModel::query()->where('deleted_at', '=', null)->pluck('chip_code');
从数据表中取得单一字段值的列表:
<?php
$roles = DB::table('roles')->lists('title');
// 重新命名title值为name
$roles = DB::table('roles')->lists('title','name');
从指定查询子句:
<?php
$users = DB::table('users')->select('name','email')->get(); $users = DB::table('users')->distinct()->get(); $users = DB::table('users')->select('name as user_name')->get(); // 增加查询子句到现有的查询中
$users = DB::table('users')->select('name')->addSelect('age')->get();
where,or的用法:
<?php
$users = DB::table('users')->where('votes', '>', 100)->orWhere('name','John')->get(); $users = DB::table('users')->whereBetween('votes',[1,100])->get(); $user = DB::table('users')->whereNotBetween('votes',[1,100])->get(); $users = DB::table('users')->whereIn('id',[1,2,3])->get();
$users = DB::table('users')->whereNotIn('id',[1,3])->get(); $users = DB::table('users')->whereNull('updated_at')->get(); $admin = DB::table('users')->whereId(1)->first();
$john = DB::table('users')->whereIdAndEmail(2, 'john@ss.com')->first();
$jane = DB::table('users')->whereNameOrAge('jane',22)->first();
?>
排序(Order By) 、分群(Group By) 及Having的用法:
<?php
$users = DB::table('users')->orderBy('name','desc')->groupBy('count')->having('count','>',100)->get(); // 偏移(offset)及限制Limit
$users = DB::table('users')->skip(10)->table(5)->get();
Joins:
<?php
DB::table('users')->join('contacts','users.id','=','contacts.user_id')
->join('orders','users.id','=','orders.user_id')
->select('user.id','contacts.phone','orders.price')
->get(); // LEFT JOIN
DB::table('users')->LEFTJoin('posts','users.id','=','posts.user_id')->get(); DB::table('users')->join('contacts',function($join){
$join->on('users.id','=','contacts.user_id')->orOn(...);
})->get(); DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
Wheres:
<?php
//有些时候你需要更高级的 where 子句,如「where exists」或嵌套的群组化参数。Laravel //的查询构造器也可以处理这样的情况:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
/*
上面的查找语法会产生下方的 SQL:
select * from users where name = 'John' or (votes > 100 and title <> 'Admin')
*/
//Exists 语法
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
/*
上面的查找语法会产生下方的 SQL:
select * from users
where exists (
select 1 from orders where orders.user_id = users.id
)
*/
whereRaw(可以加原生语句):例如:
TrGoModel::query()->whereRaw('id = 286')->first();
聚合:
$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');
原生表达式
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
whereRaw(可以加原生语句):例如:
TrGoModel::query()->whereRaw('id = 286')->first();
添加
添加数据进数据表
DB::table('users')->insert(
['email' => 'john@example.com', 'votes' => 0]
);
添加自动递增 (Auto-Incrementing) ID 的数据至数据表
如果数据表有自动递增的ID,可以使用 insertGetId 添加数据并返回该 ID: $id = DB::table('users')->insertGetId(
['email' => 'john@example.com', 'votes' => 0]
);
注意: 当使用 PostgreSQL 时,insertGetId 方法会预期自动增加的字段是以「id」为命名。 添加多个数据进数据表
DB::table('users')->insert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.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();
Unions
查询构造器也提供一个快速的方法去「合并 (union)」两个查找的结果:
$first = DB::table('users')->whereNull('first_name');
$users = DB::table('users')->whereNull('last_name')->union($first)->get();
unionAll 方法也可以使用,它与 union 方法的使用方式一样。
悲观锁定 (Pessimistic Locking)
查询构造器提供了少数函数协助你在 SELECT 语句中做到「悲观锁定」。
想要在 SELECT 语句中加上「Shard lock」,只要在查找语句中使用 sharedLock 函数:
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
要在 select 语法中使用「锁住更新(lock for update)」时,你可以使用 lockForUpdate方法:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
从数据表中分块查找数据列:
<?php
$users = DB::table('users')->chunk(100, function(){
foreach($users as $user){
return false; // 返回false停止处理接下来的数据列
} })
?>
laravel 控制器类DB类操作的更多相关文章
- laravel 数据库之DB类
// 取回数据表的第一条数据 DB::table('table')->where('key', 'value')->first(); DB::table('table')->firs ...
- nodejs操作mongodb数据库封装DB类
这个DB类也算是我经历了3个实际项目应用的,现分享出来,有需要的请借鉴批评. 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关 ...
- 封装类似thinkphp连贯操作数据库的Db类(简单版)。
<?php header("Content-Type:text/html;charset=utf-8"); /** *php操作mysql的工具类 */ class Db{ ...
- PHP——laravel之DB类->查询
DB类之查询: 满足条件的全部获取:DB::table("表名")->where("name",">","1" ...
- 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类定义 文件\ ...
- TP5 模型类和Db类的使用区别
原文:http://www.upwqy.com/details/3.html 总结 在控制器中 模型操作 get() 和 all() 只能单独使用来查询数据 想要链式操作查询数据 需要使用f ...
- Laravel5.2中Eloquent与DB类的区别是什么?
要了解这些先看看关于数据库组件的那些事儿(就是 Eloquent ORM) 数据库组件大概分了三层: 数据库连接层 查询构造层 应用层 来看一下每一层有哪些东西,分别对应文档的哪一部分: 数据库连接层 ...
- 封装自己的DB类(PHP)
封装一个DB类,用来专门操作数据库,以后凡是对数据库的操作,都由DB类的对象来实现.这样有了自己的DB类,写项目时简单的sql语句就不用每次写了,直接调用就行,很方便! 1.封装一个DB类.一个类文件 ...
随机推荐
- vlan 知识学习
背景 有人反映打印机总是提示ip冲突,经检查,打印机虽然设置了固定ip,但是所有员工在路由器都是设置了DHCP,所以会存在员工占用打印机IP 情况,在路由器添加某一段的ip不自动分配解决此问题 ...
- LINUX系统VMSTAT命令详解
linux系统vmstat命令详解 [转自 https://www.cnblogs.com/wensiyang0916/p/6514820.html] vmstat 1 1表示每秒采集一次vms ...
- Linux -- Centos6 yum安装相关问题与处理
Centos6 yum安装相关问题与处理 由于要使用yum下载文件,突然yum下载不了想要的文件,想更换yum源,结果得重新安装yum 来自本人GitHub地址https://github.com/m ...
- js的事件冒泡,事件捕获
addEventListener() 方法可以指定 "useCapture" 参数来设置传递事件类型:false→冒泡 true→捕获 默认false. ...
- java 并发 concurrent Executor
Excutor类 Executor 执行提交的对象Runnable任务. ExecutorService 一个Executor ,提供方法来管理终端和方法,可以产生Future为跟踪一个或多个异步任务 ...
- Ubuntu下 安装MiniGUI
1. 需要下载的组件 首先需要这些安装包,这些安装包可以在MiniGUI官网上下载. libminigui-gpl-3_0_12.tar.gzmg-samples-3_0_12.tar.gzfreet ...
- 四.property
将一个类的函数定义成特性以后,对象再去使用的时候obj.name,根本无法察觉自己的name是执行了一个函数然后计算出来的,这种特性的使用方式遵循了统一访问的原则 # 例一:BMI指数(bmi是计算而 ...
- vue+element ui 表格自定义样式溢出隐藏
样式 .hoveTitle { text-align: left; width: 140px; overflow: hidden; text-overflow: ellipsis; white-spa ...
- Git 头像修改 原
Git头像分两种: 第一种是直接在你当前托管的git服务网站中自定义上传头像 第二种通过第三方网站修改,基本上所有git服务网站都遵循这点,下面说的就是该模式 其实很简单!!! https://zh- ...
- python接口测试-认识GET请求
前边用工具也实现了接口自动化,但是后来很多的时候维护成本有点高.而且灵活上还是有所欠缺的. 于是,自己开始摸索学习敲码.应该有很多不对的地方或者可以优化的望各位大神勿喷,同时欢迎各位大神评论区发表自己 ...