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类.一个类文件 ...
随机推荐
- 更改Ubuntu默认python版本的方法
当你安装 Debian Linux 时,安装过程有可能同时为你提供多个可用的 Python 版本,因此系统中会存在多个 Python 的可执行二进制文件.一般Ubuntu默认的Python版本都为2. ...
- 读书笔记-JavaScript高级程序设计(1)
1.组合继承 (JavaScript 中最常用的继承模式 ) (position: page168) (书中定义了两个变量名 SuperType SubType 乍一看 感觉不太能区分,我将改为 ...
- Mysql --库和表的操作
库的增删改查 系统数据库 创建数据库 数据库的相关操作 表的操作 存储引擎介绍(有点多 很啰唆) 表的介绍 表的操作 一.系统数据库 查看系统库: show databases; nformation ...
- noj算法 堡垒问题 回溯法
描述: 城堡是一个4×4的方格,为了保卫城堡,现需要在某些格子里修建一些堡垒.城堡中的某些格子是墙,其余格子都是空格,堡垒只能建在空格里,每个堡垒都可以向上下左右四个方向射击,如果两个堡垒在同一行或同 ...
- win10安装mysql5.7.20解压版
mysql安装包可到官网下载,地址:https://dev.mysql.com/downloads/mysql 1.首先解压文件包,我这解压到E:\install_work\mysql目录下: 2.发 ...
- docker修改默认存储位置
前言:我这是默认安装的docker,所以其存放位置是/var/lib/docker,你可以发现是在/下面,之后你用docker 拉取的镜像文件也存放在这下面,这样就很快导致/空间爆满 1.修改配置文件 ...
- Redis .NET操作
Redis是一个支持数据结构更多的键值对数据库.它的值不仅可以是字符串等基本数据类型,也可以是类对象,更可以是Set.List.计数器等高级的数据结构. Memcached也可以保存类似于Set.Li ...
- 分享腾讯云的Linux服务器连接速度很慢的解决心得(原创)
最近发觉连接服务器非常慢,之前没有出现过这种情况. 我在这个腾讯云的服务器上弄了很多虚拟服务器,估计是数据量太大 造成了冗余数据较多的原因,咨询了下腾讯云的小哥, 给我了个明确的回复: 您反馈Xshe ...
- net core体系-web应用程序-4asp.net core2.0 项目实战(1)-13基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级
1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...
- Python学习(三十八)—— Djago之Ajax
转载自:http://www.cnblogs.com/yuanchenqi/articles/7638956.html 一.Ajax准备知识:json 什么是json? 定义: JSON(JavaSc ...