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类.一个类文件 ...
随机推荐
- jqGrid合并单元格
两个参数 /**合并单元格:合并指定 gridName表格的NoName 列,合并的标准是参考CellName+CellNameTwo列内单元格的值. * gridName :表格名称 * NoNam ...
- Vuex数据可视化
参考:https://gitee.com/hjm100/codes/46towe9v28a1bxfqhc7kl34 Vuex虽然能存储数据,但是一刷新就没有了,如果要实现数据持久化,就需要用vuex- ...
- 记录一下,PC端vue开发常用框架,已经用过elementUI和iview 接下来尝试另一个Muse-UI 喜欢它的点击效果
官网地址: https://muse-ui.org/#/zh-CN/installation
- 《剑指offer》把数组排成最小的数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- iOS关键词weak和assign的区别
一.区别 首先说说在什么情况下使用 weak 关键字 1.ARC中,在有可能出现循环引用的时候,往往要通过让其中一端使用weak来解决,比如:delegate 的代理属性. 2.自身已经对它有过一次强 ...
- 利用web.py快速搭建网页helloworld
访问web.py官网 http://webpy.org/ 根据网站步骤,利用 pip install web.py 若没有 PIP 则先安装pip 运行 sudo apt-get install py ...
- 末学者笔记--Linux中RAID磁盘阵列及centos7启动过程
<一>RAID概念 磁盘阵列(Redundant Arrays of Independent Disks,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意. 磁盘阵列是由很多价格较便 ...
- table行颜色设置
function renderingTable(obj){ $(obj).each(function(){ //设置奇数行颜色 $(this).find(" ...
- .net core 2.x - 缓存的四种方式
其实这些微软docs都有现成的,但是现在的人想对浮躁些,去看的不会太多,所以这里就再记录下 ,大家一起懒一起浮躁,呵呵. 0.基础知识 通过减少生成内容所需的工作,缓存可以显著提高应用的性能和可伸缩性 ...
- Excel—单元格引用
EXCEL的引用有3种:相对引用.绝对引用.混合引用 相对引用.绝对引用比较简单,就是要么拖拽后变,要么拖拽后不变 混合引用有点难度,凡是需要向右拖拽再向下拖拽的记住一定是混合引用 要将拖拽后依然不变 ...