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数据库,通过写一个子类去继承它,就可以方便的创建.管理数据库.但是当我们需要 ...
随机推荐
- HTTPS与SSL(一)
1. HTTPS HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版 ...
- 利用binlog2sql解析mysqlbinlog row行日志
binlog2sql 项目作者:曹单锋 github项目地址:https://github.com/danfengcao/binlog2sql 也可在github.com上搜索“binlog2sql” ...
- 在一个css文件中引入其他css文件
@import "./main.css";@import "./color-dark.css";@import "./reset.css";
- Json的本地写入和读取,也可以方便在开发中数据的调试
不知道小伙伴们,在开发中,数据调试的过程中,尤其是很多状态的情况下调试,是不是总是麻烦后台的小哥改变不同的状态,总感觉这样太麻烦了, 那么就可以,把数据写入到本地,然后去沙盒中,找到这个写入的文件,直 ...
- 数组使用NSUserDefaults存储的问题,
最近在做搜索记录的时候,由于搜索记录是存储在本地的,而且都是字符串,我考虑到数据量也不是太大,于是就懒的使用数据库了. 于是就想到了NSUserDefaults 存储的方式, 但是由于之间对于数组没有 ...
- python __getattr__ __setattr__
class Rectangle: def __init__(self): self.width = 0 self.height = 0 def __setattr__(self, key, value ...
- 求最大公约数和最小公倍数_python
"""写两个函数,分别求两个整数的最大公约数和最小公倍数,调用这两个函数,并输出结果.两个整数由键盘输入.""" ''' 设两个整数u和v, ...
- DongDong坐飞机
题目连接:https://ac.nowcoder.com/acm/contest/904/D 第一次研究了一下这种题型,还是比较好理解的,因为有半价次数的限制,所以要把每一中情况都写出来,dp[现在的 ...
- OO终章
一,第四单元架构设计 第一次作业:只有类图 1,重置MyClass,MyOperation等类,为使里面只有必要数据(name,id,visibility等).或方便组织数据(如MyClass作为其底 ...
- 运维自动化之Cobbler系统安装详解
原文链接 参考文档 参考文档SA们现在都知道运维自动化的重要性,尤其是对于在服务器数量按几百台.几千台增加的公司而言,单单是装系统,如果不通过自动化来完成,根本是不可想象的. 运维自动化安装方面,早期 ...