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数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
随机推荐
- 【loj2133】【NOI2015】品酒大会
Portal --> loj2133 Solution 虽然说这题貌似用后缀树之类的好像会更加简短一点..但是还是因为在智力康复所以就还是用后缀数组好了嗯(哇好感动啊难得那么顺畅写了一道noi的 ...
- Oracle中rank() over, dense_rank(), row_number() 的区别
摘自:http://www.linuxidc.com/Linux/2015-04/116349.htm Oracle 中 rank() over, dense_rank(), row_number() ...
- 51nod 1225 数学
F(n) = (n % 1) + (n % 2) + (n % 3) + ...... (n % n).其中%表示Mod,也就是余数. 例如F(6) = 6 % 1 + 6 % 2 + 6 % 3 + ...
- STL源码分析归档
1) algorithm 2) traits 3) iterator 4) list 5) function 6) rbtree 7) bitset 8) priority_queue 9) hash ...
- WPF系列之二:解耦View层控件事件与ViewModel层事件的响应
以前的做法: 1.当项目的时间比较紧迫的时候,对UI层中控件的事件的处理,往往采取的是类似Winform中最简单的做法,直接做一个事件的Handler直接去调用VM层的方法. 2.控件只有一个Comm ...
- 数据压缩算法之哈夫曼编码(HUFFMAN)的实现
HUFFMAN编码可以很有效的压缩数据,通常可以压缩20%到90%的空间(算法导论).具体的压缩率取决于数据的特性(词频).如果采取标准的语料库进行编码,一般可以得到比较满意的编码结果(对不同文件产生 ...
- ubuntu系统安装mysql二进制压缩包(tar.gz)以及navicat远程连接服务器(linux系统)
一.ubuntu安装mysql5.6二进制压缩包(tar.gz) 准备 0. 获取 mysql-5.5.15-linux2.6-i686.tar.gz 二进制安装文件 mysql 官网下载页面选择 L ...
- [吴恩达机器学习笔记]12支持向量机4核函数和标记点kernels and landmark
12.支持向量机 觉得有用的话,欢迎一起讨论相互学习~Follow Me 12.4 核函数与标记点- Kernels and landmarks 问题引入 如果你有以下的训练集,然后想去拟合其能够分开 ...
- flex属性设置详解
CSS代码中常见这样的写法:flex:1 这是flex 的缩写: flex-grow.flex-shrink.flex-basis,其取值可以考虑以下情况: 1. flex 的默认值是以上三个属性值的 ...
- phpcms添加子栏目后的读取
一个栏目下面如果没有子栏目,那么它调用的模板就是列表页模板(及list_为前缀的模板):如果一个栏目下面有子栏目,那么它调用的就是栏目首页模板(category_为前缀的模板). 所以,当你这个栏目添 ...