laravel5.2总结--数据库操作
1 配置信息
//默认的数据库
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', ''),
//更多配置
], //可以创建更多的数据库
'mysql' => [
'driver' => 'mysql_2',
'host' => env('DB_HOST', '192.168.1.2'),
'port' => env('DB_PORT', ''),
//更多配置
],
2 使用DB门面进行基本操作
DB::insert('insert into users (id, name) values (?, ?)', [, 'Dayle']);
$deleted = DB::delete('delete from users');
$affected = DB::update('update users set votes = 100 where name = ?', ['John']);
$users = DB::select('select * from users where active = ?', []);
foreach ($users as $user) {
echo $user->name;
}
DB::transaction(function () {
DB::table('users')->update(['votes' => ]);
DB::table('posts')->delete();
});
DB::beginTransaction();
if($someContion){
DB::rollback();
return false;
}
DB::commit();
DB::statement('drop table users');
3 查询构造器
3.1 获取结果
$users = DB::table('users')->get();
foreach ($users as $user) {
echo $user->name;
//操作的是对象的属性
}
$user = DB::table('users')->where('name', 'John')->first();
$email = DB::table('users')->where('name', 'John')->value('email');
DB::table('users')->chunk(, function($users) {
foreach ($users as $user) {
//
}
});
DB::table('users')->chunk(, function($users) {
// 处理记录…
return false;
});
$titles = DB::table('roles')->pluck('title');
foreach ($titles as $title) {
echo $title;
}
$roles = DB::table('roles')->pluck('title', 'name');
foreach ($roles as $name => $title) {
echo $title;
}
3.2 select的用法
$users = DB::table('users')->select('name', 'email as user_email')->get();
$users = DB::table('users')->distinct()->get();
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();
3.3 join的用法
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->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', '>', );
})
->get();
3.4 where的用法
$users = DB::table('users')->where('votes', '=', )->get();
$users = DB::table('users')->where('name', 'like', 'T%')->get();
$users = DB::table('users')->where('votes', )->get(); //省略了等号
$users = DB::table('users')->where('votes', '>', ) ->orWhere('name', 'John')->get();
$users = DB::table('users')->whereBetween('votes', [, ])->get(); //一个字段的值介于两个值之间
$users = DB::table('users')->whereNotBetween('votes', [, ])->get(); //一个字段的值落在两个值之外
$users = DB::table('users')->whereIn('id', [, , ])->get(); //字段的值包含在指定的数组之内
$users = DB::table('users')->whereNotIn('id', [, , ])->get(); //字段的值不包含在指定的数组之内
$users = DB::table('users')->whereNull('updated_at')->get(); //指定列的值为 NULL
$users = DB::table('users')->whereNotNull('updated_at')->get(); //一个列的值不为 NULL
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', )
->where('title', '<>', 'Admin');
})
->get();
select * from users where name = 'John' or (votes > and title <> 'Admin')
$users = DB::table('users') ->orderBy('name', 'desc') ->get();
$randomUser = DB::table('users') ->inRandomOrder() ->first();
$users = DB::table('users') ->groupBy('account_id') ->having('account_id', '>', ) ->get();
$users = DB::table('orders') ->select('department', DB::raw('SUM(price) as total_sales')) ->groupBy('department') ->havingRaw('SUM(price) > 2500') ->get();
$role = $request->input('role');
$users = DB::table('users')
->when($role, function ($query) use ($role) {
return $query->where('role_id', $role);
})
->get();
DB::table('users')->insert( ['email' => 'john@example.com', 'votes' => ] );
DB::table('users')->insert([ ['email' => 'taylor@example.com', 'votes' => ], ['email' => 'dayle@example.com', 'votes' => ] ]);
$id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => ] );
DB::table('users') ->where('id', ) ->update(['votes' => ]);
DB::table('users')->delete();
DB::table('users')->where('votes', '<', )->delete();
//若你希望截去整个数据表的所有数据列,并将自动递增 ID 重设为零,则可以使用 truncate 方法:
DB::table('users')->truncate();
DB::table('users')->where('votes', '>', )->sharedLock()->get();
DB::table('users')->where('votes', '>', )->lockForUpdate()->get();
laravel5.2总结--数据库操作的更多相关文章
- 如何在高并发环境下设计出无锁的数据库操作(Java版本)
一个在线2k的游戏,每秒钟并发都吓死人.传统的hibernate直接插库基本上是不可行的.我就一步步推导出一个无锁的数据库操作. 1. 并发中如何无锁. 一个很简单的思路,把并发转化成为单线程.Jav ...
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
最近用php做了点小东东,用上了ezSQL,感觉真的很ez,所以拿来跟大家分享一下~ ezSQL是一个非常好用的PHP数据库操作类.著名的开源博客WordPress的数据库操作就使用了ezSQL的My ...
- MySQL 系列(二) 你不知道的数据库操作
第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 本章内容: 查看\创建\使用\删除 数据库 用户管理及授权实战 局域网 ...
- ABP创建数据库操作步骤
1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- django数据库操作和中间件
数据库配置 django的数据库相关表配置在models.py文件中,数据库的连接相关信息配置在settings.py中 models.py相关相关参数配置 from django.db import ...
- [Android Pro] 完美Android Cursor使用例子(Android数据库操作)
reference to : http://www.ablanxue.com/prone_10575_1.html 完美 Android Cursor使用例子(Android数据库操作),Androi ...
- phpcms v9 中的数据库操作函数
1.查询 $this->select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='') 返回 ...
- Android打造属于自己的数据库操作类。
1.概述 开发Android的同学都知道sdk已经为我们提供了一个SQLiteOpenHelper类来创建和管理SQLite数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
随机推荐
- 洛谷P1397 [NOI2013]矩阵游戏(十进制矩阵快速幂)
题意 题目链接 Sol 感觉做这题只要对矩阵乘法理解的稍微一点就能做出来对于每一行构造一个矩阵A = a 1 0 b列与列之间的矩阵为B = c 1 0 d最终答案为$A^{n - ...
- Vue-watch选项
Vue ----watch 选项 用于 监听数据变化: <!DOCTYPE html> <html lang="en"> <head> < ...
- svn项目权限控制
[groups] g_manager = zhangsan g_php = lisi g_test = wangwu [/] @g_manager = rw [project:/] @g_manage ...
- linux 命令——49 at (转)
在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...
- linux 命令——41 ps(转)
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- hdu-2680 Choose the best route---dijkstra+反向存图或者建立超级源点
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意: 给你一个有向图,一个起点集合,一个终点,求最短路 解题思路: 1.自己多加一个超级 ...
- 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)
后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...
- solr数据分片相关
solr操作url 使用正常的core,使用命令生成coillection solr create_collection -c students2 -d ../server/solr/my/conf ...
- Beyond Compare在Mac下永久试用
转自 作者:忆如初 链接:https://www.jianshu.com/p/596b4463eacd 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. 亲测可用 一. ...
- Mybatis自查询递归查找子
先看一下数据库 主键id,名称product_code,父parent,和kind 设计菜单类 setter,getter Dao public interface ProductMapper { L ...