laravel5.1--数据库操作
1 配置信息
//默认的数据库
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
//更多配置
], //可以创建更多的数据库
'mysql' => [
'driver' => 'mysql_2',
'host' => env('DB_HOST', '192.168.1.2'),
'port' => env('DB_PORT', '3306'),
//更多配置
],
2 使用DB门面进行基本操作
DB::insert('insert into users (id, name) values (?, ?)', [1, '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 = ?', [1]);
foreach ($users as $user) {
echo $user->name;
}
DB::transaction(function () {
DB::table('users')->update(['votes' => 1]);
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(100, function($users) {
foreach ($users as $user) {
//
}
});
DB::table('users')->chunk(100, 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', '>', 5);
})
->get();
3.4 where的用法
$users = DB::table('users')->where('votes', '=', 100)->get();
$users = DB::table('users')->where('name', 'like', 'T%')->get();
$users = DB::table('users')->where('votes', 100)->get(); //省略了等号
$users = DB::table('users')->where('votes', '>', 100) ->orWhere('name', 'John')->get();
$users = DB::table('users')->whereBetween('votes', [1, 100])->get(); //一个字段的值介于两个值之间
$users = 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, 2, 3])->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', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
select * from users where name = 'John' or (votes > 100 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', '>', 100) ->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' => 0] );
DB::table('users')->insert([ ['email' => 'taylor@example.com', 'votes' => 0], ['email' => 'dayle@example.com', 'votes' => 0] ]);
$id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => 0] );
DB::table('users') ->where('id', 1) ->update(['votes' => 1]);
DB::table('users')->delete();
DB::table('users')->where('votes', '<', 100)->delete();
//若你希望截去整个数据表的所有数据列,并将自动递增 ID 重设为零,则可以使用 truncate 方法:
DB::table('users')->truncate();
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
laravel5.1--数据库操作的更多相关文章
- laravel5.2总结--数据库操作
1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...
- 如何在高并发环境下设计出无锁的数据库操作(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数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
随机推荐
- 【BZOJ2653】Middle(主席树)
[BZOJ2653]Middle(主席树) 题面 BZOJ 洛谷 Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整.给你 ...
- C++——内存对象 禁止产生堆对象 禁止产生栈对象
用C或C++写程序,需要更多地关注内存,这不仅仅是因为内存的分配是否合理直接影响着程序的效率和性能,更为主要的是,当我们操作内存的时候一不小心就会出现问题,而且很多时候,这些问题都是不易发觉的,比如内 ...
- Redis基操
Redis key-value类型的缓存数据库 指定IP和端口连接redis: ./redis-cli -h ip -p port Redis基本操作命令 命令 返回值 简介 ping PONG 测试 ...
- Spring MVC同时接收一个对象与List集合对象
原:https://blog.csdn.net/u011781521/article/details/77586688/ Spring MVC同时接收一个对象与List集合对象 2017年08月25日 ...
- Hdu1542 Atlantis
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- bzoj 2844: albus就是要第一个出场 高斯消元
LINK 题意:看题目不如看样例解释.给出有n个数的集合,对这些子集中的数求异或,升序统计所有子集得到的数(重复会被计入),询问一个数x,问这个数出现的第一个位置 思路:在这里要求一个所有可能出现的异 ...
- 在vm上面安装Linux系统
1 在vm上面安装Linux系统 1 以管理员的身份运行VMware: 点击VM图标然后右键属性 ,点兼容性 ---特权 等级 选择 以管理员的身份运行此软件 2 . 添加一个虚 ...
- errno错误号含义
errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...
- LintCode 395: First Will Win 2
LintCode 395: First Will Win 2 题目描述 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币 ...
- 【CodeForces】671 B. Robin Hood
[题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...